addons/isl/src/stackEdit/__tests__/reorderState.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 {CommitRev} from '../commitStackState';
b69ab319
b69ab3110import {reorderWithDeps} from '../reorderState';
b69ab3111
b69ab3112describe('reorderWithDeps', () => {
b69ab3113 const depMap = new Map<CommitRev, Set<CommitRev>>([
b69ab3114 [3 as CommitRev, new Set([2] as CommitRev[])],
b69ab3115 [4 as CommitRev, new Set([2] as CommitRev[])],
b69ab3116 [5 as CommitRev, new Set([3, 1] as CommitRev[])],
b69ab3117 ]);
b69ab3118
b69ab3119 it('moves nothing if offset is 0', () => {
b69ab3120 expect(reorderWithDeps(5, 3 as CommitRev, 0, depMap)).toMatchObject({
b69ab3121 order: [0, 1, 2, 3, 4],
b69ab3122 deps: [3],
b69ab3123 offset: 0,
b69ab3124 });
b69ab3125 });
b69ab3126
b69ab3127 it('moves down without deps', () => {
b69ab3128 expect(reorderWithDeps(5, 4 as CommitRev, -1, depMap)).toMatchObject({
b69ab3129 order: [0, 1, 2, 4, 3],
b69ab3130 deps: [4],
b69ab3131 offset: -1,
b69ab3132 });
b69ab3133 });
b69ab3134
b69ab3135 it('moves up without deps', () => {
b69ab3136 expect(reorderWithDeps(5, 0 as CommitRev, 1, depMap)).toMatchObject({
b69ab3137 order: [1, 0, 2, 3, 4],
b69ab3138 deps: [0],
b69ab3139 offset: 1,
b69ab3140 });
b69ab3141
b69ab3142 expect(reorderWithDeps(5, 0 as CommitRev, 4, depMap)).toMatchObject({
b69ab3143 order: [1, 2, 3, 4, 0],
b69ab3144 deps: [0],
b69ab3145 offset: 4,
b69ab3146 });
b69ab3147 });
b69ab3148
b69ab3149 it('bounds out of range offsets', () => {
b69ab3150 expect(reorderWithDeps(5, 3 as CommitRev, 999, new Map())).toMatchObject({
b69ab3151 order: [0, 1, 2, 4, 3],
b69ab3152 deps: [3],
b69ab3153 offset: 1,
b69ab3154 });
b69ab3155
b69ab3156 expect(reorderWithDeps(5, 3 as CommitRev, -999, new Map())).toMatchObject({
b69ab3157 order: [3, 0, 1, 2, 4],
b69ab3158 deps: [3],
b69ab3159 offset: -3,
b69ab3160 });
b69ab3161 });
b69ab3162
b69ab3163 it('moves down with deps', () => {
b69ab3164 // Move 4 to before 2, [4, 2] changed to [2, 4] for deps.
b69ab3165 expect(reorderWithDeps(5, 4 as CommitRev, -2, depMap)).toMatchObject({
b69ab3166 order: [0, 1, 2, 4, 3],
b69ab3167 deps: [2, 4],
b69ab3168 });
b69ab3169
b69ab3170 // Move 4 to before 1, [2, 4] are moved together.
b69ab3171 expect(reorderWithDeps(5, 4 as CommitRev, -3, depMap)).toMatchObject({
b69ab3172 order: [0, 2, 4, 1, 3],
b69ab3173 deps: [2, 4],
b69ab3174 });
b69ab3175
b69ab3176 // Move 5 to the bottom. 5->3, 5->1, 3->2 deps are considered.
b69ab3177 expect(reorderWithDeps(6, 5 as CommitRev, -5, depMap)).toMatchObject({
b69ab3178 order: [1, 2, 3, 5, 0, 4],
b69ab3179 deps: [1, 2, 3, 5],
b69ab3180 });
b69ab3181 });
b69ab3182
b69ab3183 it('moves up with deps', () => {
b69ab3184 // Moves 1 up and 1->5 dep is considered.
b69ab3185 expect(reorderWithDeps(6, 1 as CommitRev, 4, depMap)).toMatchObject({
b69ab3186 order: [0, 2, 3, 4, 1, 5],
b69ab3187 deps: [1, 5],
b69ab3188 });
b69ab3189
b69ab3190 // Moves 2 up and 2->3, 2->4, 3->5 deps are considered.
b69ab3191 expect(reorderWithDeps(6, 2 as CommitRev, 3, depMap)).toMatchObject({
b69ab3192 order: [0, 1, 2, 3, 4, 5],
b69ab3193 deps: [2, 3, 4, 5],
b69ab3194 });
b69ab3195 });
b69ab3196});