| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {PartialSelection} from '../partialSelection'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | describe('PartialSelection', () => { |
| b69ab31 | | | 11 | it('constructs with empty()', () => { |
| b69ab31 | | | 12 | let select = PartialSelection.empty({selectByDefault: true}); |
| b69ab31 | | | 13 | expect(select.isEverythingSelected(() => ['path1'])).toBe(true); |
| b69ab31 | | | 14 | expect(select.isEverythingSelected(() => [])).toBe(true); |
| b69ab31 | | | 15 | expect(select.isNothingSelected(() => ['path1'])).toBe(false); |
| b69ab31 | | | 16 | expect(select.isNothingSelected(() => [])).toBe(true); |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | select = PartialSelection.empty({selectByDefault: false}); |
| b69ab31 | | | 19 | expect(select.isEverythingSelected(() => ['path1'])).toBe(false); |
| b69ab31 | | | 20 | expect(select.isEverythingSelected(() => [])).toBe(true); |
| b69ab31 | | | 21 | expect(select.isNothingSelected(() => ['path1'])).toBe(true); |
| b69ab31 | | | 22 | expect(select.isNothingSelected(() => [])).toBe(true); |
| b69ab31 | | | 23 | }); |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | it('selects files using select() and deselect()', () => { |
| b69ab31 | | | 26 | [true, false].forEach(selectByDefault => { |
| b69ab31 | | | 27 | let select = PartialSelection.empty({selectByDefault}).select('path1').deselect('path2'); |
| b69ab31 | | | 28 | expect(select.isEverythingSelected(() => ['path1', 'path2'])).toBe(false); |
| b69ab31 | | | 29 | expect(select.isNothingSelected(() => ['path1', 'path2'])).toBe(false); |
| b69ab31 | | | 30 | expect(select.getSimplifiedSelection('path1')).toBe(true); |
| b69ab31 | | | 31 | expect(select.getSimplifiedSelection('path2')).toBe(false); |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | select = select.select('path2'); |
| b69ab31 | | | 34 | expect(select.getSimplifiedSelection('path2')).toBe(true); |
| b69ab31 | | | 35 | expect(select.isEverythingSelected(() => ['path1', 'path2'])).toBe(true); |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | select = select.deselect('path2').deselect('path1'); |
| b69ab31 | | | 38 | expect(select.getSimplifiedSelection('path1')).toBe(false); |
| b69ab31 | | | 39 | expect(select.isNothingSelected(() => ['path1', 'path2'])).toBe(true); |
| b69ab31 | | | 40 | }); |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | it('clear() removes selection', () => { |
| b69ab31 | | | 44 | [true, false].forEach(selectByDefault => { |
| b69ab31 | | | 45 | const select = PartialSelection.empty({selectByDefault}) |
| b69ab31 | | | 46 | .select('path1') |
| b69ab31 | | | 47 | .deselect('path2') |
| b69ab31 | | | 48 | .clear(); |
| b69ab31 | | | 49 | expect(select.getSimplifiedSelection('path1')).toBe(selectByDefault); |
| b69ab31 | | | 50 | expect(select.getSimplifiedSelection('path2')).toBe(selectByDefault); |
| b69ab31 | | | 51 | }); |
| b69ab31 | | | 52 | }); |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | it('partially selects files using startChunkSelect() and editChunkSelect()', () => { |
| b69ab31 | | | 55 | [true, false].forEach(selectByDefault => { |
| b69ab31 | | | 56 | let select = PartialSelection.empty({selectByDefault}); |
| b69ab31 | | | 57 | select = select.startChunkSelect('path1', '11\n22\n', '22\n33\n', false); |
| b69ab31 | | | 58 | expect(select.getSimplifiedSelection('path1')).toBe(false); |
| b69ab31 | | | 59 | expect(select.isNothingSelected(() => ['path1'])).toBe(true); |
| b69ab31 | | | 60 | expect(select.isEverythingSelected(() => ['path1'])).toBe(false); |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | select = select.startChunkSelect('path1', '11\n22\n', '22\n33\n', true); |
| b69ab31 | | | 63 | expect(select.getSimplifiedSelection('path1')).toBe(true); |
| b69ab31 | | | 64 | expect(select.isNothingSelected(() => ['path1'])).toBe(false); |
| b69ab31 | | | 65 | expect(select.isEverythingSelected(() => ['path1'])).toBe(true); |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | select = select.editChunkSelect('path1', chunkState => |
| b69ab31 | | | 68 | chunkState.setSelectedLines([ |
| b69ab31 | | | 69 | [0, false], // deselect '- 11'. |
| b69ab31 | | | 70 | [2, true], // select '+ 33'. |
| b69ab31 | | | 71 | ]), |
| b69ab31 | | | 72 | ); |
| b69ab31 | | | 73 | expect(select.getSimplifiedSelection('path1')).toBe('11\n22\n33\n'); |
| b69ab31 | | | 74 | expect(select.isNothingSelected(() => ['path1'])).toBe(false); |
| b69ab31 | | | 75 | expect(select.isEverythingSelected(() => ['path1'])).toBe(false); |
| b69ab31 | | | 76 | }); |
| b69ab31 | | | 77 | }); |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | it('calculates ImportStackFiles', () => { |
| b69ab31 | | | 80 | [true, false].forEach(selectByDefault => { |
| b69ab31 | | | 81 | [true, false].forEach(inverse => { |
| b69ab31 | | | 82 | const select = PartialSelection.empty({selectByDefault}) |
| b69ab31 | | | 83 | .select('path1') |
| b69ab31 | | | 84 | .deselect('path2') |
| b69ab31 | | | 85 | .startChunkSelect('path3', 'a\n', 'b\n', 'a\n') |
| b69ab31 | | | 86 | .startChunkSelect('path4', 'a\n', 'b\n', 'b\n') |
| b69ab31 | | | 87 | .startChunkSelect('path5', 'a\n', 'b\n', 'c\n'); |
| b69ab31 | | | 88 | const allPaths = ['path1', 'path2', 'path3', 'path4', 'path5', 'path6']; |
| b69ab31 | | | 89 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| b69ab31 | | | 90 | const expected: any = { |
| b69ab31 | | | 91 | path1: '.', |
| b69ab31 | | | 92 | path4: {data: inverse ? 'a\n' : 'b\n', copyFrom: '.', flags: '.'}, |
| b69ab31 | | | 93 | path5: {data: inverse ? 'b\na\n' : 'c\n', copyFrom: '.', flags: '.'}, |
| b69ab31 | | | 94 | }; |
| b69ab31 | | | 95 | if (selectByDefault) { |
| b69ab31 | | | 96 | expected.path6 = '.'; |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | expect(select.calculateImportStackFiles(allPaths, inverse)).toMatchObject(expected); |
| b69ab31 | | | 99 | }); |
| b69ab31 | | | 100 | }); |
| b69ab31 | | | 101 | }); |
| b69ab31 | | | 102 | }); |