|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 OneLiteFeather |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect } from 'vitest' |
| 18 | +import { nextTick, ref } from 'vue' |
| 19 | +import { useRowFade } from './useRowFade' |
| 20 | + |
| 21 | +// Every test builds its own keys and its own composable, so no test depends on another. |
| 22 | +const setup = (initial: string[], ready = true) => { |
| 23 | + const keys = ref(initial) |
| 24 | + const isReady = ref(ready) |
| 25 | + const { isAdded } = useRowFade(() => keys.value, () => isReady.value) |
| 26 | + return { keys, isReady, isAdded } |
| 27 | +} |
| 28 | + |
| 29 | +describe('useRowFade', () => { |
| 30 | + it('does not mark the rows of the initial load', () => { |
| 31 | + const { isAdded } = setup(['a', 'b']) |
| 32 | + |
| 33 | + expect(isAdded('a'), 'a row of the initial load does not fade').toBe(false) |
| 34 | + expect(isAdded('b'), 'a row of the initial load does not fade').toBe(false) |
| 35 | + }) |
| 36 | + |
| 37 | + it('marks a row that is added after the initial load', async () => { |
| 38 | + const { keys, isAdded } = setup(['a']) |
| 39 | + |
| 40 | + keys.value = ['a', 'b'] |
| 41 | + await nextTick() |
| 42 | + |
| 43 | + expect(isAdded('b'), 'the new row fades in').toBe(true) |
| 44 | + expect(isAdded('a'), 'the row that was there stays plain').toBe(false) |
| 45 | + }) |
| 46 | + |
| 47 | + it('does not mark the rows below a removed row', async () => { |
| 48 | + const { keys, isAdded } = setup(['a', 'b', 'c']) |
| 49 | + |
| 50 | + keys.value = ['a', 'c'] |
| 51 | + await nextTick() |
| 52 | + |
| 53 | + expect(isAdded('a'), 'a removal marks nothing').toBe(false) |
| 54 | + expect(isAdded('c'), 'a removal marks nothing').toBe(false) |
| 55 | + }) |
| 56 | + |
| 57 | + it('does not mark rows that only change their order', async () => { |
| 58 | + const { keys, isAdded } = setup(['a', 'b']) |
| 59 | + |
| 60 | + keys.value = ['b', 'a'] |
| 61 | + await nextTick() |
| 62 | + |
| 63 | + expect(isAdded('a'), 'a sort is not an addition').toBe(false) |
| 64 | + expect(isAdded('b'), 'a sort is not an addition').toBe(false) |
| 65 | + }) |
| 66 | + |
| 67 | + it('waits for the load and treats the rows that arrive with it as the initial load', async () => { |
| 68 | + const { keys, isReady, isAdded } = setup([], false) |
| 69 | + |
| 70 | + keys.value = ['a', 'b'] |
| 71 | + isReady.value = true |
| 72 | + await nextTick() |
| 73 | + |
| 74 | + expect(isAdded('a'), 'the first loaded rows do not fade').toBe(false) |
| 75 | + expect(isAdded('b'), 'the first loaded rows do not fade').toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + it('treats the next load as an initial load again', async () => { |
| 79 | + const { keys, isReady, isAdded } = setup(['a']) |
| 80 | + |
| 81 | + isReady.value = false |
| 82 | + await nextTick() |
| 83 | + keys.value = ['x', 'y'] |
| 84 | + isReady.value = true |
| 85 | + await nextTick() |
| 86 | + |
| 87 | + expect(isAdded('x'), 'the rows of another directory do not fade').toBe(false) |
| 88 | + expect(isAdded('y'), 'the rows of another directory do not fade').toBe(false) |
| 89 | + }) |
| 90 | + |
| 91 | + it('marks a row again when it is removed and added back', async () => { |
| 92 | + const { keys, isAdded } = setup(['a', 'b']) |
| 93 | + |
| 94 | + keys.value = ['a'] |
| 95 | + await nextTick() |
| 96 | + keys.value = ['a', 'b'] |
| 97 | + await nextTick() |
| 98 | + |
| 99 | + expect(isAdded('b'), 'a row that returns is an added row').toBe(true) |
| 100 | + }) |
| 101 | +}) |
0 commit comments