1import fs from 'fs';
2import { vol } from 'memfs';
3
4import { copyFilePathToPathAsync, removeFile } from '../fs';
5jest.mock('fs');
6
7describe(copyFilePathToPathAsync, () => {
8  beforeAll(() => {
9    jest.spyOn(fs.promises, 'readFile');
10    jest.spyOn(fs.promises, 'writeFile');
11  });
12  afterAll(() => {
13    vol.reset();
14  });
15
16  it(`copies a single file into a nested location`, async () => {
17    const projectRoot = '/';
18    const CONTENTS = '{ foobar }';
19    vol.fromJSON(
20      {
21        'google-services.json': CONTENTS,
22      },
23      projectRoot
24    );
25
26    await copyFilePathToPathAsync('/google-services.json', '/android/app/google-services.json');
27
28    expect(fs.promises.readFile).toHaveBeenLastCalledWith('/google-services.json');
29
30    expect(fs.promises.writeFile).toHaveBeenLastCalledWith(
31      '/android/app/google-services.json',
32      expect.anything()
33    );
34
35    expect(vol.toJSON(projectRoot)).toEqual({
36      // New
37      '/android/app/google-services.json': CONTENTS,
38      // Old -- both should still exist
39      '/google-services.json': CONTENTS,
40    });
41  });
42});
43
44describe(removeFile, () => {
45  afterAll(() => {
46    vol.reset();
47  });
48
49  it(`removes a single file`, () => {
50    const projectRoot = '/';
51    vol.fromJSON(
52      {
53        'google-services.json': '{ foobar }',
54      },
55      projectRoot
56    );
57
58    expect(removeFile('/google-services.json')).toBe(true);
59
60    expect(vol.toJSON(projectRoot)).toEqual({});
61  });
62  it(`returns false if the requested file is missing`, () => {
63    vol.fromJSON({}, '/');
64
65    expect(removeFile('/google-services.json')).toBe(false);
66  });
67
68  it(`does not remove non-empty directories`, async () => {
69    const projectRoot = '/';
70    vol.fromJSON(
71      {
72        '/android/app/file.txt': '{}',
73      },
74      projectRoot
75    );
76
77    expect(() => removeFile('/android/app')).toThrow(/Dir not empty/);
78  });
79});
80