addons/isl/src/__tests__/partialSelection.test.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {PartialSelection} from '../partialSelection';
b69ab319
b69ab3110describe('PartialSelection', () => {
b69ab3111 it('constructs with empty()', () => {
b69ab3112 let select = PartialSelection.empty({selectByDefault: true});
b69ab3113 expect(select.isEverythingSelected(() => ['path1'])).toBe(true);
b69ab3114 expect(select.isEverythingSelected(() => [])).toBe(true);
b69ab3115 expect(select.isNothingSelected(() => ['path1'])).toBe(false);
b69ab3116 expect(select.isNothingSelected(() => [])).toBe(true);
b69ab3117
b69ab3118 select = PartialSelection.empty({selectByDefault: false});
b69ab3119 expect(select.isEverythingSelected(() => ['path1'])).toBe(false);
b69ab3120 expect(select.isEverythingSelected(() => [])).toBe(true);
b69ab3121 expect(select.isNothingSelected(() => ['path1'])).toBe(true);
b69ab3122 expect(select.isNothingSelected(() => [])).toBe(true);
b69ab3123 });
b69ab3124
b69ab3125 it('selects files using select() and deselect()', () => {
b69ab3126 [true, false].forEach(selectByDefault => {
b69ab3127 let select = PartialSelection.empty({selectByDefault}).select('path1').deselect('path2');
b69ab3128 expect(select.isEverythingSelected(() => ['path1', 'path2'])).toBe(false);
b69ab3129 expect(select.isNothingSelected(() => ['path1', 'path2'])).toBe(false);
b69ab3130 expect(select.getSimplifiedSelection('path1')).toBe(true);
b69ab3131 expect(select.getSimplifiedSelection('path2')).toBe(false);
b69ab3132
b69ab3133 select = select.select('path2');
b69ab3134 expect(select.getSimplifiedSelection('path2')).toBe(true);
b69ab3135 expect(select.isEverythingSelected(() => ['path1', 'path2'])).toBe(true);
b69ab3136
b69ab3137 select = select.deselect('path2').deselect('path1');
b69ab3138 expect(select.getSimplifiedSelection('path1')).toBe(false);
b69ab3139 expect(select.isNothingSelected(() => ['path1', 'path2'])).toBe(true);
b69ab3140 });
b69ab3141 });
b69ab3142
b69ab3143 it('clear() removes selection', () => {
b69ab3144 [true, false].forEach(selectByDefault => {
b69ab3145 const select = PartialSelection.empty({selectByDefault})
b69ab3146 .select('path1')
b69ab3147 .deselect('path2')
b69ab3148 .clear();
b69ab3149 expect(select.getSimplifiedSelection('path1')).toBe(selectByDefault);
b69ab3150 expect(select.getSimplifiedSelection('path2')).toBe(selectByDefault);
b69ab3151 });
b69ab3152 });
b69ab3153
b69ab3154 it('partially selects files using startChunkSelect() and editChunkSelect()', () => {
b69ab3155 [true, false].forEach(selectByDefault => {
b69ab3156 let select = PartialSelection.empty({selectByDefault});
b69ab3157 select = select.startChunkSelect('path1', '11\n22\n', '22\n33\n', false);
b69ab3158 expect(select.getSimplifiedSelection('path1')).toBe(false);
b69ab3159 expect(select.isNothingSelected(() => ['path1'])).toBe(true);
b69ab3160 expect(select.isEverythingSelected(() => ['path1'])).toBe(false);
b69ab3161
b69ab3162 select = select.startChunkSelect('path1', '11\n22\n', '22\n33\n', true);
b69ab3163 expect(select.getSimplifiedSelection('path1')).toBe(true);
b69ab3164 expect(select.isNothingSelected(() => ['path1'])).toBe(false);
b69ab3165 expect(select.isEverythingSelected(() => ['path1'])).toBe(true);
b69ab3166
b69ab3167 select = select.editChunkSelect('path1', chunkState =>
b69ab3168 chunkState.setSelectedLines([
b69ab3169 [0, false], // deselect '- 11'.
b69ab3170 [2, true], // select '+ 33'.
b69ab3171 ]),
b69ab3172 );
b69ab3173 expect(select.getSimplifiedSelection('path1')).toBe('11\n22\n33\n');
b69ab3174 expect(select.isNothingSelected(() => ['path1'])).toBe(false);
b69ab3175 expect(select.isEverythingSelected(() => ['path1'])).toBe(false);
b69ab3176 });
b69ab3177 });
b69ab3178
b69ab3179 it('calculates ImportStackFiles', () => {
b69ab3180 [true, false].forEach(selectByDefault => {
b69ab3181 [true, false].forEach(inverse => {
b69ab3182 const select = PartialSelection.empty({selectByDefault})
b69ab3183 .select('path1')
b69ab3184 .deselect('path2')
b69ab3185 .startChunkSelect('path3', 'a\n', 'b\n', 'a\n')
b69ab3186 .startChunkSelect('path4', 'a\n', 'b\n', 'b\n')
b69ab3187 .startChunkSelect('path5', 'a\n', 'b\n', 'c\n');
b69ab3188 const allPaths = ['path1', 'path2', 'path3', 'path4', 'path5', 'path6'];
b69ab3189 // eslint-disable-next-line @typescript-eslint/no-explicit-any
b69ab3190 const expected: any = {
b69ab3191 path1: '.',
b69ab3192 path4: {data: inverse ? 'a\n' : 'b\n', copyFrom: '.', flags: '.'},
b69ab3193 path5: {data: inverse ? 'b\na\n' : 'c\n', copyFrom: '.', flags: '.'},
b69ab3194 };
b69ab3195 if (selectByDefault) {
b69ab3196 expected.path6 = '.';
b69ab3197 }
b69ab3198 expect(select.calculateImportStackFiles(allPaths, inverse)).toMatchObject(expected);
b69ab3199 });
b69ab31100 });
b69ab31101 });
b69ab31102});