|
| 1 | +import { it, describe, expect, afterEach, vi } from 'vitest'; |
| 2 | +import { gray } from 'colorette'; |
| 3 | +import { render, cleanup as cleanupMountedReactTrees, act } from '@testing-library/react'; |
| 4 | +import { iterateFormatted, Iterate } from '../../src/index.js'; |
| 5 | +import { pipe } from '../utils/pipe.js'; |
| 6 | +import { asyncIterToArray } from '../utils/asyncIterToArray.js'; |
| 7 | +import { IterableChannelTestHelper } from '../utils/IterableChannelTestHelper.js'; |
| 8 | + |
| 9 | +afterEach(() => { |
| 10 | + cleanupMountedReactTrees(); |
| 11 | +}); |
| 12 | + |
| 13 | +describe('`iterateFormatted` function', () => { |
| 14 | + it(gray('When called on some plain value it formats and returns that on the spot'), () => { |
| 15 | + const multiFormattedPlainValue = pipe( |
| 16 | + 'a', |
| 17 | + $ => iterateFormatted($, (value, i) => `${value} formatted once (idx: ${i})`), |
| 18 | + $ => iterateFormatted($, (value, i) => `${value} and formatted twice (idx: ${i})`) |
| 19 | + ); |
| 20 | + expect(multiFormattedPlainValue).toStrictEqual( |
| 21 | + 'a formatted once (idx: 0) and formatted twice (idx: 0)' |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + it( |
| 26 | + gray( |
| 27 | + 'When the resulting object is iterated manually (without the library tools) it still has the provided formatting applied' |
| 28 | + ), |
| 29 | + async () => { |
| 30 | + const multiFormattedIter = pipe( |
| 31 | + (async function* () { |
| 32 | + yield* ['a', 'b', 'c']; |
| 33 | + })(), |
| 34 | + $ => iterateFormatted($, (value, i) => `${value} formatted once (idx: ${i})`), |
| 35 | + $ => iterateFormatted($, (value, i) => `${value} and formatted twice (idx: ${i})`) |
| 36 | + ); |
| 37 | + |
| 38 | + const yielded = await asyncIterToArray(multiFormattedIter); |
| 39 | + |
| 40 | + expect(yielded).toStrictEqual([ |
| 41 | + 'a formatted once (idx: 0) and formatted twice (idx: 0)', |
| 42 | + 'b formatted once (idx: 1) and formatted twice (idx: 1)', |
| 43 | + 'c formatted once (idx: 2) and formatted twice (idx: 2)', |
| 44 | + ]); |
| 45 | + } |
| 46 | + ); |
| 47 | + |
| 48 | + it( |
| 49 | + gray( |
| 50 | + 'When the wrapped source is used normally with library tools it is rendered and formatted correctly' |
| 51 | + ), |
| 52 | + async () => { |
| 53 | + const channel = new IterableChannelTestHelper<string>(); |
| 54 | + |
| 55 | + const rendered = render( |
| 56 | + <Iterate |
| 57 | + value={pipe( |
| 58 | + channel, |
| 59 | + $ => iterateFormatted($, (value, i) => `${value} formatted once (idx: ${i})`), |
| 60 | + $ => iterateFormatted($, (value, i) => `${value} and formatted twice (idx: ${i})`) |
| 61 | + )} |
| 62 | + > |
| 63 | + {next => <p>Rendered: {next.value}</p>} |
| 64 | + </Iterate> |
| 65 | + ); |
| 66 | + |
| 67 | + expect(rendered.container.innerHTML).toStrictEqual('<p>Rendered: </p>'); |
| 68 | + |
| 69 | + for (const [i, value] of ['a', 'b', 'c'].entries()) { |
| 70 | + await act(() => channel.put(value)); |
| 71 | + expect(rendered.container.innerHTML).toStrictEqual( |
| 72 | + `<p>Rendered: ${value} formatted once (idx: ${i}) and formatted twice (idx: ${i})</p>` |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + ); |
| 77 | + |
| 78 | + it( |
| 79 | + gray( |
| 80 | + 'When re-rendering with a new wrapped iterable each time, as long as they wrap the same source iterable, the same source iteration process will persist across these re-renderings' |
| 81 | + ), |
| 82 | + async () => { |
| 83 | + const [channel1, channel2] = [ |
| 84 | + new IterableChannelTestHelper<string>(), |
| 85 | + new IterableChannelTestHelper<string>(), |
| 86 | + ]; |
| 87 | + |
| 88 | + const [channelReturnSpy1, channelReturnSpy2] = [ |
| 89 | + vi.spyOn(channel1, 'return'), |
| 90 | + vi.spyOn(channel2, 'return'), |
| 91 | + ]; |
| 92 | + |
| 93 | + const rebuildTestContent = (it: AsyncIterable<string>) => ( |
| 94 | + <Iterate |
| 95 | + value={pipe( |
| 96 | + it, |
| 97 | + $ => iterateFormatted($, (value, i) => `${value} formatted once (idx: ${i})`), |
| 98 | + $ => iterateFormatted($, (value, i) => `${value} and formatted twice (idx: ${i})`) |
| 99 | + )} |
| 100 | + > |
| 101 | + {next => <p>Rendered: {next.value}</p>} |
| 102 | + </Iterate> |
| 103 | + ); |
| 104 | + |
| 105 | + const rendered = render(<></>); |
| 106 | + |
| 107 | + rendered.rerender(rebuildTestContent(channel1)); |
| 108 | + expect(channelReturnSpy1).not.toHaveBeenCalled(); |
| 109 | + |
| 110 | + rendered.rerender(rebuildTestContent(channel1)); |
| 111 | + expect(channelReturnSpy1).not.toHaveBeenCalled(); |
| 112 | + |
| 113 | + rendered.rerender(rebuildTestContent(channel2)); |
| 114 | + expect(channelReturnSpy1).toHaveBeenCalledOnce(); |
| 115 | + expect(channelReturnSpy2).not.toHaveBeenCalled(); |
| 116 | + |
| 117 | + rendered.rerender(rebuildTestContent(channel2)); |
| 118 | + expect(channelReturnSpy2).not.toHaveBeenCalled(); |
| 119 | + } |
| 120 | + ); |
| 121 | + |
| 122 | + it( |
| 123 | + gray( |
| 124 | + 'Always the latest closure passed in as the format function will be the one to format the next-arriving source value' |
| 125 | + ), |
| 126 | + async () => { |
| 127 | + const channel = new IterableChannelTestHelper<string>(); |
| 128 | + |
| 129 | + const Wrapper = (props: { outerValue: string }) => ( |
| 130 | + <Iterate |
| 131 | + value={pipe( |
| 132 | + channel, |
| 133 | + $ => |
| 134 | + iterateFormatted( |
| 135 | + $, |
| 136 | + (value, i) => `${value} formatted once (idx: ${i}, outer val: ${props.outerValue})` |
| 137 | + ), |
| 138 | + $ => |
| 139 | + iterateFormatted( |
| 140 | + $, |
| 141 | + (value, i) => |
| 142 | + `${value} and formatted twice (idx: ${i}, outer val: ${props.outerValue})` |
| 143 | + ) |
| 144 | + )} |
| 145 | + > |
| 146 | + {next => <p>Rendered: {next.value}</p>} |
| 147 | + </Iterate> |
| 148 | + ); |
| 149 | + |
| 150 | + const rendered = render(<></>); |
| 151 | + |
| 152 | + for (const [i, [nextYield, nextProp]] of [ |
| 153 | + ['yield_a', 'prop_a'], |
| 154 | + ['yield_b', 'prop_b'], |
| 155 | + ['yield_c', 'prop_c'], |
| 156 | + ].entries()) { |
| 157 | + rendered.rerender(<Wrapper outerValue={nextProp} />); |
| 158 | + await act(() => channel.put(nextYield)); |
| 159 | + |
| 160 | + expect(rendered.container.innerHTML).toStrictEqual( |
| 161 | + `<p>Rendered: ${nextYield} formatted once (idx: ${i}, outer val: ${nextProp}) and formatted twice (idx: ${i}, outer val: ${nextProp})</p>` |
| 162 | + ); |
| 163 | + } |
| 164 | + } |
| 165 | + ); |
| 166 | +}); |
0 commit comments