1import { resolve } from 'path';
2
3import { getMainActivity, readAndroidManifestAsync } from '../Manifest';
4import { getOrientation, setAndroidOrientation } from '../Orientation';
5
6const fixturesPath = resolve(__dirname, 'fixtures');
7const sampleManifestPath = resolve(fixturesPath, 'react-native-AndroidManifest.xml');
8
9describe('Android orientation', () => {
10  it(`returns null if no orientation is provided`, () => {
11    expect(getOrientation({})).toBe(null);
12  });
13
14  it(`returns orientation if provided`, () => {
15    expect(getOrientation({ orientation: 'landscape' })).toMatch('landscape');
16  });
17
18  describe('File changes', () => {
19    let androidManifestJson;
20    it('adds orientation attribute if not present', async () => {
21      androidManifestJson = await readAndroidManifestAsync(sampleManifestPath);
22      androidManifestJson = await setAndroidOrientation(
23        { orientation: 'landscape' },
24        androidManifestJson
25      );
26
27      const mainActivity = getMainActivity(androidManifestJson);
28
29      expect(mainActivity.$['android:screenOrientation']).toMatch('landscape');
30    });
31
32    it('replaces orientation attribute if present', async () => {
33      androidManifestJson = await readAndroidManifestAsync(sampleManifestPath);
34
35      androidManifestJson = await setAndroidOrientation(
36        { orientation: 'portrait' },
37        androidManifestJson
38      );
39
40      const mainActivity = getMainActivity(androidManifestJson);
41
42      expect(mainActivity.$['android:screenOrientation']).toMatch('portrait');
43    });
44
45    it('replaces orientation with unspecified if provided default', async () => {
46      androidManifestJson = await readAndroidManifestAsync(sampleManifestPath);
47      androidManifestJson = await setAndroidOrientation(
48        { orientation: 'default' },
49        androidManifestJson
50      );
51
52      const mainActivity = getMainActivity(androidManifestJson);
53
54      expect(mainActivity.$['android:screenOrientation']).toMatch('unspecified');
55    });
56  });
57});
58