|
| 1 | +import {getTemplateVersion} from '../npm'; |
| 2 | +import assert from 'assert'; |
| 3 | + |
| 4 | +let ref: any; |
| 5 | + |
| 6 | +global.fetch = jest.fn(); |
| 7 | + |
| 8 | +function fetchReturn(json: any): void { |
| 9 | + assert(global.fetch != null, 'You forgot to backup global.fetch!'); |
| 10 | + // @ts-ignore |
| 11 | + global.fetch = jest.fn(() => |
| 12 | + Promise.resolve({json: () => Promise.resolve(json)}), |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +describe('getTemplateVersion', () => { |
| 17 | + beforeEach(() => { |
| 18 | + ref = global.fetch; |
| 19 | + }); |
| 20 | + afterEach(() => { |
| 21 | + global.fetch = ref; |
| 22 | + }); |
| 23 | + |
| 24 | + it('should order matching versions with the most recent first', async () => { |
| 25 | + const VERSION = '0.75.1'; |
| 26 | + fetchReturn({ |
| 27 | + versions: { |
| 28 | + '3.2.1': {scripts: {version: VERSION}}, |
| 29 | + '1.0.0': {scripts: {version: '0.75.0'}}, |
| 30 | + '1.2.3': {scripts: {version: VERSION}}, |
| 31 | + }, |
| 32 | + time: { |
| 33 | + '3.2.1': '2024-08-15T00:00:00.000Z', |
| 34 | + '1.0.0': '2024-08-15T10:10:10.000Z', |
| 35 | + '1.2.3': '2024-08-16T00:00:00.000Z', // Last published version |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(await getTemplateVersion(VERSION)).toEqual('1.2.3'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should matching latest MAJOR.MINOR if MAJOR.MINOR.PATCH has no match', async () => { |
| 43 | + fetchReturn({ |
| 44 | + versions: { |
| 45 | + '3.2.1': {scripts: {version: '0.75.1'}}, |
| 46 | + '3.2.2': {scripts: {version: '0.75.2'}}, |
| 47 | + }, |
| 48 | + time: { |
| 49 | + '3.2.1': '2024-08-15T00:00:00.000Z', |
| 50 | + '3.2.2': '2024-08-16T00:00:00.000Z', // Last published version |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(await getTemplateVersion('0.75.3')).toEqual('3.2.2'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should NOT matching when MAJOR.MINOR is not found', async () => { |
| 58 | + fetchReturn({ |
| 59 | + versions: { |
| 60 | + '3.2.1': {scripts: {version: '0.75.1'}}, |
| 61 | + '3.2.2': {scripts: {version: '0.75.2'}}, |
| 62 | + }, |
| 63 | + time: { |
| 64 | + '3.2.1': '2024-08-15T00:00:00.000Z', |
| 65 | + '3.2.2': '2024-08-16T00:00:00.000Z', // Last published version |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(await getTemplateVersion('0.76.0')).toEqual(undefined); |
| 70 | + }); |
| 71 | + |
| 72 | + it('ignores packages that have weird script version entries', async () => { |
| 73 | + fetchReturn({ |
| 74 | + versions: { |
| 75 | + '1': {}, |
| 76 | + '2': {scripts: {}}, |
| 77 | + '3': {scripts: {version: 'echo "not a semver entry"'}}, |
| 78 | + win: {scripts: {version: '0.75.2'}}, |
| 79 | + }, |
| 80 | + time: { |
| 81 | + '1': '2024-08-14T00:00:00.000Z', |
| 82 | + win: '2024-08-15T00:00:00.000Z', |
| 83 | + // These would normally both beat '3' on time: |
| 84 | + '2': '2024-08-16T00:00:00.000Z', |
| 85 | + '3': '2024-08-16T00:00:00.000Z', |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(await getTemplateVersion('0.75.2')).toEqual('win'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('support `version` and `reactNativeVersion` entries from npm', async () => { |
| 93 | + fetchReturn({ |
| 94 | + versions: { |
| 95 | + '3.2.1': {scripts: {version: '0.75.1'}}, |
| 96 | + '3.2.2': {scripts: {reactNativeVersion: '0.75.2'}}, |
| 97 | + }, |
| 98 | + time: { |
| 99 | + '3.2.1': '2024-08-15T00:00:00.000Z', |
| 100 | + '3.2.2': '2024-08-16T00:00:00.000Z', // Last published version |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(await getTemplateVersion('0.75.2')).toEqual('3.2.2'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('prefers `reactNativeVersion` over `version` entries from npm', async () => { |
| 108 | + fetchReturn({ |
| 109 | + versions: { |
| 110 | + '3.2.1': {scripts: {version: '0.75.1'}}, |
| 111 | + '3.2.2': { |
| 112 | + scripts: { |
| 113 | + reactNativeVersion: '0.75.2', |
| 114 | + version: 'should prefer the other one', |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + time: { |
| 119 | + '3.2.1': '2024-08-15T00:00:00.000Z', |
| 120 | + '3.2.2': '2024-08-16T00:00:00.000Z', // Last published version |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + expect(await getTemplateVersion('0.75.2')).toEqual('3.2.2'); |
| 125 | + }); |
| 126 | +}); |
0 commit comments