1import fetch from 'node-fetch';
2
3import { stripAnsi } from '../ansi';
4import { isUrlAvailableAsync } from '../url';
5import {
6  getBundleIdWarningAsync,
7  getPackageNameWarningAsync,
8  validateBundleId,
9  validatePackage,
10} from '../validateApplicationId';
11
12jest.mock('../url');
13jest.mock('node-fetch');
14
15describe(validateBundleId, () => {
16  it(`validates`, () => {
17    expect(validateBundleId('bacon')).toBe(true);
18    expect(validateBundleId('...b.a.-c.0.n...')).toBe(true);
19    expect(validateBundleId('.')).toBe(true);
20    expect(validateBundleId('. ..')).toBe(false);
21    expect(validateBundleId('_')).toBe(false);
22    expect(validateBundleId(',')).toBe(false);
23  });
24});
25
26describe(validatePackage, () => {
27  it(`validates`, () => {
28    expect(validatePackage('bacon.com.hey')).toBe(true);
29    expect(validatePackage('bacon')).toBe(false);
30    expect(validatePackage('...b.a.-c.0.n...')).toBe(false);
31    expect(validatePackage('.')).toBe(false);
32    expect(validatePackage('. ..')).toBe(false);
33    expect(validatePackage('_')).toBe(false);
34    expect(validatePackage(',')).toBe(false);
35  });
36});
37
38describe(getBundleIdWarningAsync, () => {
39  it(`returns null if the URL cannot be reached`, async () => {
40    (isUrlAvailableAsync as jest.Mock).mockResolvedValueOnce(false);
41    expect(await getBundleIdWarningAsync('bacon')).toBe(null);
42  });
43  it(`returns warning if in use`, async () => {
44    (isUrlAvailableAsync as jest.Mock).mockResolvedValueOnce(true);
45
46    (fetch as any).mockImplementationOnce(() => ({
47      json() {
48        return Promise.resolve({
49          resultCount: 1,
50          results: [
51            {
52              trackName: 'Pillar Valley',
53              sellerName: 'Evan Bacon',
54              kind: 'software',
55              artistName: 'Evan Bacon',
56              genres: ['Games', 'Entertainment', 'Family', 'Casual'],
57            },
58          ],
59        });
60      },
61    }));
62    expect(
63      stripAnsi(await getBundleIdWarningAsync('com.bacon.pillarvalley'))
64    ).toMatchInlineSnapshot(
65      `"⚠️  The app Pillar Valley by Evan Bacon is already using com.bacon.pillarvalley"`
66    );
67  });
68});
69describe(getPackageNameWarningAsync, () => {
70  it(`returns null if the URL cannot be reached`, async () => {
71    (isUrlAvailableAsync as jest.Mock).mockResolvedValueOnce(false);
72    expect(await getPackageNameWarningAsync('bacon')).toBe(null);
73  });
74  it(`returns warning if in use`, async () => {
75    (isUrlAvailableAsync as jest.Mock).mockResolvedValueOnce(true);
76
77    (fetch as any).mockImplementationOnce(() => ({
78      status: 200,
79    }));
80    expect(
81      stripAnsi(await getPackageNameWarningAsync('com.bacon.pillarvalley'))
82    ).toMatchInlineSnapshot(
83      `"⚠️  The package com.bacon.pillarvalley is already in use. Learn more: https://play.google.com/store/apps/details?id=com.bacon.pillarvalley"`
84    );
85  });
86});
87