1import { vol } from 'memfs';
2
3import { parseXMLAsync } from '../../utils/XML';
4import { fileExistsAsync } from '../../utils/modules';
5import { getResourceXMLPathAsync } from '../Paths';
6import { getObjectAsResourceGroup, readResourcesXMLAsync } from '../Resources';
7
8jest.mock('fs');
9
10describe(getResourceXMLPathAsync, () => {
11  beforeAll(async () => {
12    vol.fromJSON(
13      {
14        './android/app/src/main/res/values/colors.xml': '<resources></resources>',
15        // './android/app/src/main/res/values-night/colors.xml': '<resources></resources>',
16      },
17      '/app'
18    );
19    vol.fromJSON(
20      {
21        // no files -- specifically no android folder
22      },
23      '/managed'
24    );
25  });
26  afterAll(async () => {
27    vol.reset();
28  });
29
30  it(`returns a fallback value for a missing file`, async () => {
31    const path = await getResourceXMLPathAsync('/app', { name: 'colors', kind: 'values-night' });
32    // ensure the file is missing so the test works as expected.
33    expect(await fileExistsAsync(path)).toBe(false);
34    // read the file with a default fallback
35    expect(await readResourcesXMLAsync({ path })).toStrictEqual({ resources: {} });
36  });
37  it(`returns a default value for an XML file`, async () => {
38    const path = await getResourceXMLPathAsync('/app', { name: 'colors', kind: 'values' });
39    // ensure the file exists so the test works as expected.
40    expect(await fileExistsAsync(path)).toBe(true);
41    // read the file with a default fallback
42    expect(await readResourcesXMLAsync({ path })).toStrictEqual({ resources: {} });
43  });
44});
45
46describe(getObjectAsResourceGroup, () => {
47  it(`matches parsed xml`, async () => {
48    // Parsed from a string for DX readability
49    const styles = await parseXMLAsync(
50      [
51        '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
52        '<resources>',
53        '  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">',
54        '    <item name="key">value</item>',
55        '    <item name="foo">bar</item>',
56        '  </style>',
57        '</resources>',
58      ].join('\n')
59    );
60
61    expect(
62      getObjectAsResourceGroup({
63        name: 'AppTheme',
64        parent: 'Theme.AppCompat.Light.NoActionBar',
65        item: { key: 'value', foo: 'bar' },
66      })
67    ).toStrictEqual((styles.resources as any).style[0]);
68  });
69});
70