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