1import { vol } from 'memfs';
2
3import {
4  ensureValidPlatforms,
5  resolvePlatformOption,
6  resolveSkipDependencyUpdate,
7  resolvePackageManagerOptions,
8  resolveTemplateOption,
9} from '../resolveOptions';
10
11describe(resolvePackageManagerOptions, () => {
12  it(`resolves`, () => {
13    expect(resolvePackageManagerOptions({ '--yarn': true })).toEqual({
14      npm: undefined,
15      pnpm: undefined,
16      yarn: true,
17    });
18  });
19  it(`asserts mutually exclusive arguments`, () => {
20    expect(() => resolvePackageManagerOptions({ '--npm': true, '--pnpm': true })).toThrow();
21    expect(() => resolvePackageManagerOptions({ '--no-install': true, '--yarn': true })).toThrow();
22    expect(() =>
23      resolvePackageManagerOptions({
24        '--npm': true,
25        '--pnpm': true,
26        '--no-install': true,
27        '--yarn': true,
28      })
29    ).toThrow();
30  });
31});
32
33describe(resolvePlatformOption, () => {
34  const platform = process.platform;
35
36  afterEach(() => {
37    Object.defineProperty(process, 'platform', {
38      value: platform,
39    });
40  });
41
42  it('returns the correct platforms', () => {
43    expect(resolvePlatformOption('ios')).toEqual(['ios']);
44    expect(resolvePlatformOption('android')).toEqual(['android']);
45  });
46  it('returns the correct platforms (darwin)', () => {
47    Object.defineProperty(process, 'platform', {
48      value: 'darwin',
49    });
50
51    expect(resolvePlatformOption('all')).toEqual(['android', 'ios']);
52  });
53  it('returns the correct platforms (win32)', () => {
54    Object.defineProperty(process, 'platform', {
55      value: 'win32',
56    });
57
58    expect(resolvePlatformOption('all')).toEqual(['android']);
59  });
60  it('throws on unknown platform', () => {
61    expect(() => resolvePlatformOption('foo')).toThrowError(
62      'Unsupported platform "foo". Options are: ios, android, all'
63    );
64  });
65});
66
67describe(resolveSkipDependencyUpdate, () => {
68  it('splits dependencies', () => {
69    expect(resolveSkipDependencyUpdate(undefined)).toEqual([]);
70    expect(resolveSkipDependencyUpdate('')).toEqual([]);
71    expect(resolveSkipDependencyUpdate('foo')).toEqual(['foo']);
72    expect(resolveSkipDependencyUpdate('foo,bar')).toEqual(['foo', 'bar']);
73  });
74});
75
76describe(resolveTemplateOption, () => {
77  beforeAll(() => {
78    vol.fromJSON({
79      '/foo': '',
80    });
81  });
82  afterAll(() => {
83    vol.reset();
84  });
85  it('resolves a URL template', () => {
86    expect(resolveTemplateOption('http://foo')).toEqual('http://foo');
87  });
88  it('asserts a missing file path template', () => {
89    expect(() => resolveTemplateOption('bacon/bar')).toThrowError(
90      /template file does not exist: .*bacon\/bar/
91    );
92  });
93  it('resolves a file path template', () => {
94    expect(resolveTemplateOption('/foo')).toEqual('/foo');
95  });
96});
97
98describe(ensureValidPlatforms, () => {
99  const platform = process.platform;
100  const warning = console.warn;
101
102  beforeAll(() => {
103    console.warn = jest.fn();
104  });
105  afterAll(() => {
106    console.warn = warning;
107  });
108  afterEach(() => {
109    Object.defineProperty(process, 'platform', {
110      value: platform,
111    });
112  });
113  it(`bails on windows if only ios is passed`, async () => {
114    Object.defineProperty(process, 'platform', {
115      value: 'win32',
116    });
117    expect(ensureValidPlatforms(['ios', 'android'])).toStrictEqual(['android']);
118  });
119  it(`allows ios on all platforms except windows`, async () => {
120    Object.defineProperty(process, 'platform', {
121      value: 'other',
122    });
123    expect(ensureValidPlatforms(['ios', 'android'])).toStrictEqual(['ios', 'android']);
124  });
125});
126