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