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

test-utils.test.ts

107 lines2.4 KB

1import { describe, expect, it } 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 it.each(testCases)(
36 'should generate permutations of $input',
37 ({ input, expected }) => {
38 const permutations = generatePermutations(input)
39 expect(permutations).toEqual(expected)
40 },
41 )
42 })
43​
44 describe('generatePartialCombinations', () => {
45 const testCases = [
46 {
47 input: ['a', 'b', 'c'],
48 minLength: 2,
49 expected: [
50 ['a', 'b'],
51 ['a', 'c'],
52 ['b', 'c'],
53 ['a', 'b', 'c'],
54 ],
55 },
56 {
57 input: ['a', 'b'],
58 expected: [['a', 'b']],
59 minLength: 2,
60 },
61 {
62 input: ['a'],
63 expected: [],
64 minLength: 2,
65 },
66 {
67 input: ['a'],
68 expected: [['a']],
69 minLength: 1,
70 },
71 {
72 input: ['a'],
73 expected: [[], ['a']],
74 minLength: 0,
75 },
76 ]
77 it.each(testCases)(
78 'should generate combinations of $input with at least $minLength items',
79 ({ input, minLength, expected }) => {
80 const combinations = generatePartialCombinations(input, minLength)
81 expectArrayEqualIgnoreOrder(combinations, expected)
82 },
83 )
84 })
85​
86 describe('generateInterleavedCombinations', () => {
87 const testCases = [
88 {
89 data: ['a', 'b'],
90 additional: ['x'],
91 expected: [
92 ['a', 'b'],
93 ['x', 'a', 'b'],
94 ['a', 'x', 'b'],
95 ['a', 'b', 'x'],
96 ],
97 },
98 ]
99 it.each(testCases)(
100 'should interleave $additional into $data',
101 ({ data, additional, expected }) => {
102 const combinations = generateInterleavedCombinations(data, additional)
103 expectArrayEqualIgnoreOrder(combinations, expected)
104 },
105 )
106 })
107})