addons/shared/__tests__/SplitDiffView/organizeLinesIntoGroups.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 type {Hunk} from 'diff';
b69ab319
b69ab3110import {structuredPatch} from 'diff';
b69ab3111import organizeLinesIntoGroups from '../../SplitDiffView/organizeLinesIntoGroups';
b69ab3112
b69ab3113test('file with only one line that is changed (no context)', () => {
b69ab3114 const hunks = diffIntoHunks(['lowerCamelCase'], ['UpperCamelCase']);
b69ab3115 expect(hunks.length).toBe(1);
b69ab3116 const groups = organizeLinesIntoGroups(hunks[0].lines);
b69ab3117 expect(groups).toEqual([{common: [], removed: ['lowerCamelCase'], added: ['UpperCamelCase']}]);
b69ab3118});
b69ab3119
b69ab3120test('file with only first line changed', () => {
b69ab3121 const hunks = diffIntoHunks(['lowerCamelCase', 'a', 'b', 'c'], ['UpperCamelCase', 'a', 'b', 'c']);
b69ab3122 expect(hunks.length).toBe(1);
b69ab3123 const groups = organizeLinesIntoGroups(hunks[0].lines);
b69ab3124 expect(groups).toEqual([
b69ab3125 {common: [], removed: ['lowerCamelCase'], added: ['UpperCamelCase']},
b69ab3126 {common: ['a', 'b', 'c'], removed: [], added: []},
b69ab3127 ]);
b69ab3128});
b69ab3129
b69ab3130test('file with only last line changed', () => {
b69ab3131 const hunks = diffIntoHunks(['a', 'b', 'c', 'lowerCamelCase'], ['a', 'b', 'c', 'UpperCamelCase']);
b69ab3132 expect(hunks.length).toBe(1);
b69ab3133 const groups = organizeLinesIntoGroups(hunks[0].lines);
b69ab3134 expect(groups).toEqual([
b69ab3135 {common: ['a', 'b', 'c'], removed: ['lowerCamelCase'], added: ['UpperCamelCase']},
b69ab3136 ]);
b69ab3137});
b69ab3138
b69ab3139test('a mix of changed lines', () => {
b69ab3140 const hunks = diffIntoHunks(
b69ab3141 ['...', 'The', 'quick', 'fox', 'jumped', 'over', 'dog.', 'THE END'],
b69ab3142 ['The', 'quick', 'BROWN', 'fox', 'jumps', 'over', 'the lazy dog.', 'THE END'],
b69ab3143 );
b69ab3144 expect(hunks.length).toBe(1);
b69ab3145 const groups = organizeLinesIntoGroups(hunks[0].lines);
b69ab3146 expect(groups).toEqual([
b69ab3147 {
b69ab3148 common: [],
b69ab3149 removed: ['...'],
b69ab3150 added: [],
b69ab3151 },
b69ab3152 {
b69ab3153 common: ['The', 'quick'],
b69ab3154 removed: [],
b69ab3155 added: ['BROWN'],
b69ab3156 },
b69ab3157 {
b69ab3158 common: ['fox'],
b69ab3159 removed: ['jumped'],
b69ab3160 added: ['jumps'],
b69ab3161 },
b69ab3162 {
b69ab3163 common: ['over'],
b69ab3164 removed: ['dog.'],
b69ab3165 added: ['the lazy dog.'],
b69ab3166 },
b69ab3167 {
b69ab3168 common: ['THE END'],
b69ab3169 removed: [],
b69ab3170 added: [],
b69ab3171 },
b69ab3172 ]);
b69ab3173});
b69ab3174
b69ab3175function diffIntoHunks(oldLines: string[], newLines: string[], context = 3): Hunk[] {
b69ab3176 const oldText = oldLines.join('\n') + '\n';
b69ab3177 const newText = newLines.join('\n') + '\n';
b69ab3178 const parsedDiff = structuredPatch('old.txt', 'new.txt', oldText, newText, undefined, undefined, {
b69ab3179 context,
b69ab3180 });
b69ab3181 return parsedDiff.hunks;
b69ab3182}