1import { compileMockModWithResultsAsync } from '../../plugins/__tests__/mockMods';
2import { withGradleProperties } from '../../plugins/android-plugins';
3import {
4  updateAndroidBuildPropertiesFromConfig,
5  updateAndroidBuildProperty,
6  withJsEngineGradleProps,
7} from '../BuildProperties';
8import { parsePropertiesFile, PropertiesItem } from '../Properties';
9
10jest.mock('../../plugins/android-plugins');
11
12const HERMES_PROP_KEY = 'hermesEnabled';
13
14describe(withJsEngineGradleProps, () => {
15  it('set the property from shared `jsEngine` config', async () => {
16    const { modResults } = await compileMockModWithResultsAsync(
17      { jsEngine: 'hermes' },
18      {
19        plugin: withJsEngineGradleProps,
20        mod: withGradleProperties,
21        modResults: [],
22      }
23    );
24    expect(modResults).toContainEqual({
25      type: 'property',
26      key: HERMES_PROP_KEY,
27      value: 'true',
28    });
29  });
30
31  it('set the property from platform override `jsEngine`', async () => {
32    const { modResults } = await compileMockModWithResultsAsync(
33      { jsEngine: 'hermes', android: { jsEngine: 'jsc' } },
34      {
35        plugin: withJsEngineGradleProps,
36        mod: withGradleProperties,
37        modResults: [],
38      }
39    );
40    expect(modResults).toContainEqual({
41      type: 'property',
42      key: HERMES_PROP_KEY,
43      value: 'false',
44    });
45  });
46
47  it('overwrite the property if an old property is existed', async () => {
48    const originalGradleProperties = parsePropertiesFile(`
49android.useAndroidX=true
50android.enableJetifier=true
51hermesEnabled=false
52`);
53
54    const { modResults } = await compileMockModWithResultsAsync(
55      { android: { jsEngine: 'hermes' } },
56      {
57        plugin: withJsEngineGradleProps,
58        mod: withGradleProperties,
59        modResults: originalGradleProperties,
60      }
61    );
62    expect(modResults).toContainEqual({
63      type: 'property',
64      key: HERMES_PROP_KEY,
65      value: 'true',
66    });
67  });
68});
69
70describe(updateAndroidBuildPropertiesFromConfig, () => {
71  it('should respect `propValueGetter` order', () => {
72    const gradleProperties = [];
73    const configToPropertyRules = [
74      {
75        propName: HERMES_PROP_KEY,
76        propValueGetter: (config) =>
77          ((config.android?.jsEngine ?? config.jsEngine ?? 'hermes') === 'hermes').toString(),
78      },
79    ];
80
81    expect(
82      updateAndroidBuildPropertiesFromConfig(
83        { jsEngine: 'hermes', android: { jsEngine: 'jsc' } },
84        gradleProperties,
85        configToPropertyRules
86      )
87    ).toContainEqual({
88      type: 'property',
89      key: HERMES_PROP_KEY,
90      value: 'false',
91    });
92
93    expect(
94      updateAndroidBuildPropertiesFromConfig(
95        { jsEngine: 'jsc' },
96        gradleProperties,
97        configToPropertyRules
98      )
99    ).toContainEqual({
100      type: 'property',
101      key: HERMES_PROP_KEY,
102      value: 'false',
103    });
104
105    expect(
106      updateAndroidBuildPropertiesFromConfig({}, gradleProperties, configToPropertyRules)
107    ).toContainEqual({
108      type: 'property',
109      key: HERMES_PROP_KEY,
110      value: 'true',
111    });
112  });
113});
114
115describe(updateAndroidBuildProperty, () => {
116  it('should merge properties', () => {
117    const gradleProperties: PropertiesItem[] = [
118      { type: 'property', key: 'foo', value: 'foo' },
119      { type: 'property', key: 'bar', value: 'bar' },
120      { type: 'property', key: 'name', value: 'oldName' },
121    ];
122    expect(updateAndroidBuildProperty(gradleProperties, 'name', 'newName')).toEqual([
123      { type: 'property', key: 'foo', value: 'foo' },
124      { type: 'property', key: 'bar', value: 'bar' },
125      { type: 'property', key: 'name', value: 'newName' },
126    ]);
127  });
128
129  it('should keep original property when `value` is null by default', () => {
130    const gradleProperties: PropertiesItem[] = [
131      { type: 'property', key: 'foo', value: 'foo' },
132      { type: 'property', key: 'bar', value: 'bar' },
133    ];
134    expect(updateAndroidBuildProperty(gradleProperties, 'bar', null)).toEqual([
135      { type: 'property', key: 'foo', value: 'foo' },
136      { type: 'property', key: 'bar', value: 'bar' },
137    ]);
138  });
139
140  it('should remove original property when `value` is null when `removePropWhenValueIsNull` is true', () => {
141    const gradleProperties: PropertiesItem[] = [
142      { type: 'property', key: 'foo', value: 'foo' },
143      { type: 'property', key: 'bar', value: 'bar' },
144    ];
145    expect(
146      updateAndroidBuildProperty(gradleProperties, 'bar', null, {
147        removePropWhenValueIsNull: true,
148      })
149    ).toEqual([{ type: 'property', key: 'foo', value: 'foo' }]);
150  });
151});
152