|
1 | 1 | import { R } from '@mobily/ts-belt'; |
2 | 2 | import 'expect-more-jest'; |
| 3 | +import { shuffle } from '../../../../../test/shuffle'; |
3 | 4 | import { getHighestVersion } from './get-highest-version'; |
4 | 5 |
|
5 | | -const shuffle = (array: string[]): string[] => { |
6 | | - for (let i = array.length - 1; i > 0; i--) { |
7 | | - const j = Math.floor(Math.random() * (i + 1)); |
8 | | - [array[i], array[j]] = [array[j], array[i]]; |
9 | | - } |
10 | | - return array; |
11 | | -}; |
12 | | - |
13 | 6 | describe('getHighestVersion', () => { |
14 | | - it('returns the newest version from an array of versions', () => { |
15 | | - const a = ['<1.0.0']; |
16 | | - const b = shuffle([...a, '<=1.0.0']); |
17 | | - const c = shuffle([...b, '1']); |
18 | | - const d = shuffle([...c, '1.0.0']); |
19 | | - const e = shuffle([...d, '~1.0.0']); |
20 | | - const f = shuffle([...e, '1.x.x']); |
21 | | - const g = shuffle([...f, '^1.0.0']); |
22 | | - const h = shuffle([...g, '>=1.0.0']); |
23 | | - const i = shuffle([...h, '>1.0.0']); |
24 | | - const j = shuffle([...i, '*']); |
25 | | - // valid semver |
| 7 | + const a = ['<1.0.0']; |
| 8 | + const b = shuffle([...a, '<=1.0.0']); |
| 9 | + const c = shuffle([...b, '1']); |
| 10 | + const d = shuffle([...c, '1.0.0']); |
| 11 | + const e = shuffle([...d, '~1.0.0']); |
| 12 | + const f = shuffle([...e, '1.x.x']); |
| 13 | + const g = shuffle([...f, '^1.0.0']); |
| 14 | + const h = shuffle([...g, '>=1.0.0']); |
| 15 | + const i = shuffle([...h, '>1.0.0']); |
| 16 | + const j = shuffle([...i, '*']); |
| 17 | + |
| 18 | + // "1" and "1.0.0" are equal and first match wins |
| 19 | + const eitherFormat = expect.stringMatching(/^(1|1\.0\.0)$/); |
| 20 | + |
| 21 | + it('returns "<1.0.0" when it is the only version', () => { |
26 | 22 | expect(getHighestVersion(a)).toEqual(R.Ok('<1.0.0')); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns "<=1.0.0" when added', () => { |
27 | 26 | expect(getHighestVersion(b)).toEqual(R.Ok('<=1.0.0')); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns "1" when added', () => { |
28 | 30 | expect(getHighestVersion(c)).toEqual(R.Ok('1')); |
29 | | - expect(getHighestVersion(d)).toEqual( |
30 | | - // "1" and "1.0.0" are equal and first match wins |
31 | | - R.Ok(expect.stringMatching(/^(1|1\.0\.0)$/)), |
32 | | - ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns "1.0.0" when added', () => { |
| 34 | + expect(getHighestVersion(d)).toEqual(R.Ok(eitherFormat)); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns "~1.0.0" when added', () => { |
33 | 38 | expect(getHighestVersion(e)).toEqual(R.Ok('~1.0.0')); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns "1.x.x" when added', () => { |
34 | 42 | expect(getHighestVersion(f)).toEqual(R.Ok('1.x.x')); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns "^1.0.0" when added', () => { |
35 | 46 | expect(getHighestVersion(g)).toEqual(R.Ok('^1.0.0')); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns ">=1.0.0" when added', () => { |
36 | 50 | expect(getHighestVersion(h)).toEqual(R.Ok('>=1.0.0')); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns ">1.0.0" when added', () => { |
37 | 54 | expect(getHighestVersion(i)).toEqual(R.Ok('>1.0.0')); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns "*" when added', () => { |
38 | 58 | expect(getHighestVersion(j)).toEqual(R.Ok('*')); |
39 | 59 | }); |
40 | 60 | }); |
0 commit comments