123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- import forEach from 'lodash/forEach'
- import filter from 'lodash/filter'
- import invoke from 'lodash/invoke'
- import { mock, verify, instance, deepEqual, when, anything, anyString } from 'ts-mockito';
-
- import { LayoutTreeParser } from './LayoutTreeParser';
- import { LayoutTreeCrawler } from './LayoutTreeCrawler';
- import { Store } from '../components/Store';
- import { Commands } from './Commands';
- import { CommandsObserver } from '../events/CommandsObserver';
- import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
- import { OptionsProcessor } from './OptionsProcessor';
- import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
- import { Options } from '../interfaces/Options';
-
- describe('Commands', () => {
- let uut: Commands;
- let mockedNativeCommandsSender: NativeCommandsSender;
- let mockedStore: Store;
- let commandsObserver: CommandsObserver;
- let mockedUniqueIdProvider: UniqueIdProvider;
-
- beforeEach(() => {
- mockedNativeCommandsSender = mock(NativeCommandsSender);
- mockedUniqueIdProvider = mock(UniqueIdProvider);
- when(mockedUniqueIdProvider.generate(anything())).thenCall((prefix) => `${prefix}+UNIQUE_ID`);
- const uniqueIdProvider = instance(mockedUniqueIdProvider);
- mockedStore = mock(Store);
- commandsObserver = new CommandsObserver(uniqueIdProvider);
-
- const mockedOptionsProcessor = mock(OptionsProcessor);
- const optionsProcessor = instance(mockedOptionsProcessor) as OptionsProcessor;
-
- uut = new Commands(
- mockedStore,
- instance(mockedNativeCommandsSender),
- new LayoutTreeParser(uniqueIdProvider),
- new LayoutTreeCrawler(instance(mockedStore), optionsProcessor),
- commandsObserver,
- uniqueIdProvider,
- optionsProcessor
- );
- });
-
- describe('setRoot', () => {
- it('sends setRoot to native after parsing into a correct layout tree', () => {
- uut.setRoot({
- root: { component: { name: 'com.example.MyScreen' } }
- });
- verify(
- mockedNativeCommandsSender.setRoot(
- 'setRoot+UNIQUE_ID',
- deepEqual({
- root: {
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- children: [],
- data: { name: 'com.example.MyScreen', options: {}, passProps: undefined }
- },
- modals: [],
- overlays: []
- })
- )
- ).called();
- });
-
- it('returns a promise with the resolved layout', async () => {
- when(mockedNativeCommandsSender.setRoot(anything(), anything())).thenResolve(
- 'the resolved layout'
- );
- const result = await uut.setRoot({ root: { component: { name: 'com.example.MyScreen' } } });
- expect(result).toEqual('the resolved layout');
- });
-
- it('inputs modals and overlays', () => {
- uut.setRoot({
- root: { component: { name: 'com.example.MyScreen' } },
- modals: [{ component: { name: 'com.example.MyModal' } }],
- overlays: [{ component: { name: 'com.example.MyOverlay' } }]
- });
- verify(
- mockedNativeCommandsSender.setRoot(
- 'setRoot+UNIQUE_ID',
- deepEqual({
- root: {
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- children: [],
- data: {
- name: 'com.example.MyScreen',
- options: {},
- passProps: undefined
- }
- },
- modals: [
- {
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- children: [],
- data: {
- name: 'com.example.MyModal',
- options: {},
- passProps: undefined
- }
- }
- ],
- overlays: [
- {
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- children: [],
- data: {
- name: 'com.example.MyOverlay',
- options: {},
- passProps: undefined
- }
- }
- ]
- })
- )
- ).called();
- });
- });
-
- describe('mergeOptions', () => {
- it('passes options for component', () => {
- uut.mergeOptions('theComponentId', { blurOnUnmount: true });
- verify(
- mockedNativeCommandsSender.mergeOptions(
- 'theComponentId',
- deepEqual({ blurOnUnmount: true })
- )
- ).called();
- });
- });
-
- describe('updateProps', () => {
- it('delegates to store', () => {
- uut.updateProps('theComponentId', {someProp: 'someValue'});
- verify(mockedStore.updateProps('theComponentId', deepEqual({someProp: 'someValue'})));
- });
-
- it('notifies commands observer', () => {
- uut.updateProps('theComponentId', {someProp: 'someValue'});
- verify(commandsObserver.notify('updateProps', deepEqual({componentId: 'theComponentId', props: {someProp: 'someValue'}})));
- });
- });
-
- describe('showModal', () => {
- it('sends command to native after parsing into a correct layout tree', () => {
- uut.showModal({ component: { name: 'com.example.MyScreen' } });
- verify(
- mockedNativeCommandsSender.showModal(
- 'showModal+UNIQUE_ID',
- deepEqual({
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- data: {
- name: 'com.example.MyScreen',
- options: {},
- passProps: undefined
- },
- children: []
- })
- )
- ).called();
- });
-
- it('returns a promise with the resolved layout', async () => {
- when(mockedNativeCommandsSender.showModal(anything(), anything())).thenResolve(
- 'the resolved layout'
- );
- const result = await uut.showModal({ component: { name: 'com.example.MyScreen' } });
- expect(result).toEqual('the resolved layout');
- });
- });
-
- describe('dismissModal', () => {
- it('sends command to native', () => {
- uut.dismissModal('myUniqueId', {});
- verify(
- mockedNativeCommandsSender.dismissModal(
- 'dismissModal+UNIQUE_ID',
- 'myUniqueId',
- deepEqual({})
- )
- ).called();
- });
-
- it('returns a promise with the id', async () => {
- when(
- mockedNativeCommandsSender.dismissModal(anyString(), anything(), anything())
- ).thenResolve('the id');
- const result = await uut.dismissModal('myUniqueId');
- expect(result).toEqual('the id');
- });
- });
-
- describe('dismissAllModals', () => {
- it('sends command to native', () => {
- uut.dismissAllModals({});
- verify(
- mockedNativeCommandsSender.dismissAllModals('dismissAllModals+UNIQUE_ID', deepEqual({}))
- ).called();
- });
-
- it('returns a promise with the id', async () => {
- when(mockedNativeCommandsSender.dismissAllModals(anyString(), anything())).thenResolve(
- 'the id'
- );
- const result = await uut.dismissAllModals();
- expect(result).toEqual('the id');
- });
- });
-
- describe('push', () => {
- it('resolves with the parsed layout', async () => {
- when(mockedNativeCommandsSender.push(anyString(), anyString(), anything())).thenResolve(
- 'the resolved layout'
- );
- const result = await uut.push('theComponentId', {
- component: { name: 'com.example.MyScreen' }
- });
- expect(result).toEqual('the resolved layout');
- });
-
- it('parses into correct layout node and sends to native', () => {
- uut.push('theComponentId', { component: { name: 'com.example.MyScreen' } });
- verify(
- mockedNativeCommandsSender.push(
- 'push+UNIQUE_ID',
- 'theComponentId',
- deepEqual({
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- data: {
- name: 'com.example.MyScreen',
- options: {},
- passProps: undefined
- },
- children: []
- })
- )
- ).called();
- });
- });
-
- describe('pop', () => {
- it('pops a component, passing componentId', () => {
- uut.pop('theComponentId', {});
- verify(
- mockedNativeCommandsSender.pop('pop+UNIQUE_ID', 'theComponentId', deepEqual({}))
- ).called();
- });
- it('pops a component, passing componentId and options', () => {
- const options: Options = { popGesture: true };
- uut.pop('theComponentId', options);
- verify(mockedNativeCommandsSender.pop('pop+UNIQUE_ID', 'theComponentId', options)).called();
- });
-
- it('pop returns a promise that resolves to componentId', async () => {
- when(mockedNativeCommandsSender.pop(anyString(), anyString(), anything())).thenResolve(
- 'theComponentId'
- );
- const result = await uut.pop('theComponentId', {});
- expect(result).toEqual('theComponentId');
- });
- });
-
- describe('popTo', () => {
- it('pops all components until the passed Id is top', () => {
- uut.popTo('theComponentId', {});
- verify(
- mockedNativeCommandsSender.popTo('popTo+UNIQUE_ID', 'theComponentId', deepEqual({}))
- ).called();
- });
-
- it('returns a promise that resolves to targetId', async () => {
- when(mockedNativeCommandsSender.popTo(anyString(), anyString(), anything())).thenResolve(
- 'theComponentId'
- );
- const result = await uut.popTo('theComponentId');
- expect(result).toEqual('theComponentId');
- });
- });
-
- describe('popToRoot', () => {
- it('pops all components to root', () => {
- uut.popToRoot('theComponentId', {});
- verify(
- mockedNativeCommandsSender.popToRoot('popToRoot+UNIQUE_ID', 'theComponentId', deepEqual({}))
- ).called();
- });
-
- it('returns a promise that resolves to targetId', async () => {
- when(mockedNativeCommandsSender.popToRoot(anyString(), anyString(), anything())).thenResolve(
- 'theComponentId'
- );
- const result = await uut.popToRoot('theComponentId');
- expect(result).toEqual('theComponentId');
- });
- });
-
- describe('setStackRoot', () => {
- it('parses into correct layout node and sends to native', () => {
- uut.setStackRoot('theComponentId', [{ component: { name: 'com.example.MyScreen' } }]);
- verify(
- mockedNativeCommandsSender.setStackRoot(
- 'setStackRoot+UNIQUE_ID',
- 'theComponentId',
- deepEqual([
- {
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- data: {
- name: 'com.example.MyScreen',
- options: {},
- passProps: undefined
- },
- children: []
- }
- ])
- )
- ).called();
- });
- });
-
- describe('showOverlay', () => {
- it('sends command to native after parsing into a correct layout tree', () => {
- uut.showOverlay({ component: { name: 'com.example.MyScreen' } });
- verify(
- mockedNativeCommandsSender.showOverlay(
- 'showOverlay+UNIQUE_ID',
- deepEqual({
- type: 'Component',
- id: 'Component+UNIQUE_ID',
- data: {
- name: 'com.example.MyScreen',
- options: {},
- passProps: undefined
- },
- children: []
- })
- )
- ).called();
- });
-
- it('resolves with the component id', async () => {
- when(mockedNativeCommandsSender.showOverlay(anyString(), anything())).thenResolve(
- 'Component1'
- );
- const result = await uut.showOverlay({ component: { name: 'com.example.MyScreen' } });
- expect(result).toEqual('Component1');
- });
- });
-
- describe('dismissOverlay', () => {
- it('check promise returns true', async () => {
- when(mockedNativeCommandsSender.dismissOverlay(anyString(), anyString())).thenResolve(true);
- const result = await uut.dismissOverlay('Component1');
- verify(mockedNativeCommandsSender.dismissOverlay(anyString(), anyString())).called();
- expect(result).toEqual(true);
- });
-
- it('send command to native with componentId', () => {
- uut.dismissOverlay('Component1');
- verify(
- mockedNativeCommandsSender.dismissOverlay('dismissOverlay+UNIQUE_ID', 'Component1')
- ).called();
- });
- });
-
- describe('notifies commandsObserver', () => {
- let cb: any;
- let mockedLayoutTreeParser: LayoutTreeParser;
- let mockedLayoutTreeCrawler: LayoutTreeCrawler;
- let anotherMockedUniqueIdProvider: UniqueIdProvider;
-
- beforeEach(() => {
- cb = jest.fn();
- mockedLayoutTreeParser = mock(LayoutTreeParser);
- mockedLayoutTreeCrawler = mock(LayoutTreeCrawler);
- commandsObserver.register(cb);
- const mockedOptionsProcessor = mock(OptionsProcessor) as OptionsProcessor;
- anotherMockedUniqueIdProvider = mock(UniqueIdProvider);
- when(anotherMockedUniqueIdProvider.generate(anything())).thenCall(
- (prefix) => `${prefix}+UNIQUE_ID`
- );
-
- uut = new Commands(
- mockedStore,
- mockedNativeCommandsSender,
- instance(mockedLayoutTreeParser),
- instance(mockedLayoutTreeCrawler),
- commandsObserver,
- instance(anotherMockedUniqueIdProvider),
- instance(mockedOptionsProcessor)
- );
- });
-
- function getAllMethodsOfUut() {
- const uutFns = Object.getOwnPropertyNames(Commands.prototype);
- const methods = filter(uutFns, (fn) => fn !== 'constructor');
- expect(methods.length).toBeGreaterThan(1);
- return methods;
- }
-
- describe('passes correct params', () => {
- const argsForMethodName: Record<string, any[]> = {
- setRoot: [{}],
- setDefaultOptions: [{}],
- mergeOptions: ['id', {}],
- updateProps: ['id', {}],
- showModal: [{}],
- dismissModal: ['id', {}],
- dismissAllModals: [{}],
- push: ['id', {}],
- pop: ['id', {}],
- popTo: ['id', {}],
- popToRoot: ['id', {}],
- setStackRoot: ['id', [{}]],
- showOverlay: [{}],
- dismissOverlay: ['id'],
- getLaunchArgs: ['id']
- };
- const paramsForMethodName: Record<string, object> = {
- setRoot: {
- commandId: 'setRoot+UNIQUE_ID',
- layout: { root: null, modals: [], overlays: [] }
- },
- setDefaultOptions: { options: {} },
- mergeOptions: { componentId: 'id', options: {} },
- updateProps: { componentId: 'id', props: {} },
- showModal: { commandId: 'showModal+UNIQUE_ID', layout: null },
- dismissModal: { commandId: 'dismissModal+UNIQUE_ID', componentId: 'id', mergeOptions: {} },
- dismissAllModals: { commandId: 'dismissAllModals+UNIQUE_ID', mergeOptions: {} },
- push: { commandId: 'push+UNIQUE_ID', componentId: 'id', layout: null },
- pop: { commandId: 'pop+UNIQUE_ID', componentId: 'id', mergeOptions: {} },
- popTo: { commandId: 'popTo+UNIQUE_ID', componentId: 'id', mergeOptions: {} },
- popToRoot: { commandId: 'popToRoot+UNIQUE_ID', componentId: 'id', mergeOptions: {} },
- setStackRoot: {
- commandId: 'setStackRoot+UNIQUE_ID',
- componentId: 'id',
- layout: [null]
- },
- showOverlay: { commandId: 'showOverlay+UNIQUE_ID', layout: null },
- dismissOverlay: { commandId: 'dismissOverlay+UNIQUE_ID', componentId: 'id' },
- getLaunchArgs: { commandId: 'getLaunchArgs+UNIQUE_ID' }
- };
- forEach(getAllMethodsOfUut(), (m) => {
- it(`for ${m}`, () => {
- expect(argsForMethodName).toHaveProperty(m);
- expect(paramsForMethodName).toHaveProperty(m);
- invoke(uut, m, ...argsForMethodName[m]);
- expect(cb).toHaveBeenCalledTimes(1);
- expect(cb).toHaveBeenCalledWith(m, paramsForMethodName[m]);
- });
- });
- });
- });
- });
|