Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

test-utils.test.ts

104 lines2.3 KB

1import { describe, expect, test } from 'vitest'
2import {
3 expectArrayEqualIgnoreOrder,
4 generateInterleavedCombinations,
5 generatePartialCombinations,
6 generatePermutations,
7} from './test-utils'
8​
9describe('test-utils', () => {
10 describe('generatePermutations', () => {
11 const testCases = [
12 {
13 input: ['a', 'b', 'c'],
14 expected: [
15 ['a', 'b', 'c'],
16 ['a', 'c', 'b'],
17 ['b', 'a', 'c'],
18 ['b', 'c', 'a'],
19 ['c', 'a', 'b'],
20 ['c', 'b', 'a'],
21 ],
22 },
23 {
24 input: ['a', 'b'],
25 expected: [
26 ['a', 'b'],
27 ['b', 'a'],
28 ],
29 },
30 {
31 input: ['a'],
32 expected: [['a']],
33 },
34 ]
35 test.each(testCases)('$input $expected', ({ input, expected }) => {
36 const permutations = generatePermutations(input)
37 expect(permutations).toEqual(expected)
38 })
39 })
40​
41 describe('generatePartialCombinations', () => {
42 const testCases = [
43 {
44 input: ['a', 'b', 'c'],
45 minLength: 2,
46 expected: [
47 ['a', 'b'],
48 ['a', 'c'],
49 ['b', 'c'],
50 ['a', 'b', 'c'],
51 ],
52 },
53 {
54 input: ['a', 'b'],
55 expected: [['a', 'b']],
56 minLength: 2,
57 },
58 {
59 input: ['a'],
60 expected: [],
61 minLength: 2,
62 },
63 {
64 input: ['a'],
65 expected: [['a']],
66 minLength: 1,
67 },
68 {
69 input: ['a'],
70 expected: [[], ['a']],
71 minLength: 0,
72 },
73 ]
74 test.each(testCases)(
75 '$input $minLength $expected',
76 ({ input, minLength, expected }) => {
77 const combinations = generatePartialCombinations(input, minLength)
78 expectArrayEqualIgnoreOrder(combinations, expected)
79 },
80 )
81 })
82​
83 describe('generateInterleavedCombinations', () => {
84 const testCases = [
85 {
86 data: ['a', 'b'],
87 additional: ['x'],
88 expected: [
89 ['a', 'b'],
90 ['x', 'a', 'b'],
91 ['a', 'x', 'b'],
92 ['a', 'b', 'x'],
93 ],
94 },
95 ]
96 test.each(testCases)(
97 '$input $expected',
98 ({ data, additional, expected }) => {
99 const combinations = generateInterleavedCombinations(data, additional)
100 expectArrayEqualIgnoreOrder(combinations, expected)
101 },
102 )
103 })
104})