Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Jul 30, 2019
1 parent 4761620 commit adcb6cc
Show file tree
Hide file tree
Showing 36 changed files with 1,051 additions and 205 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"@kbn/pm": "1.0.0",
"@kbn/test-subj-selector": "0.2.1",
"@kbn/ui-framework": "1.0.0",
"@types/history": "^4.7.2",
"@types/json-stable-stringify": "^1.0.32",
"@types/lodash.clonedeep": "^4.5.4",
"@types/react-grid-layout": "^0.16.7",
Expand Down Expand Up @@ -166,6 +167,7 @@
"handlebars": "4.1.2",
"hapi": "^17.5.3",
"hapi-auth-cookie": "^9.0.0",
"history": "^4.9.0",
"hjson": "3.1.2",
"hoek": "^5.0.4",
"http-proxy-agent": "^2.1.0",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions src/core/public/application/application_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@
* under the License.
*/

import { Subject } from 'rxjs';

import { capabilitiesServiceMock } from './capabilities/capabilities_service.mock';
import { ApplicationService, ApplicationSetup, ApplicationStart } from './application_service';
import { ApplicationService } from './application_service';
import { ApplicationSetup, ApplicationStart, App } from './types';

type ApplicationServiceContract = PublicMethodsOf<ApplicationService>;

const createSetupContractMock = (): jest.Mocked<ApplicationSetup> => ({
registerApp: jest.fn(),
registerLegacyApp: jest.fn(),
registerMountContext: jest.fn(),
});

const createStartContractMock = (): jest.Mocked<ApplicationStart> => ({
const createStartContractMock = (legacyMode = false): jest.Mocked<ApplicationStart> => ({
...capabilitiesServiceMock.createStartContract(),
currentApp$: new Subject<App | undefined>(),
getComponent: jest.fn(),
legacyMode,
navigateToApp: jest.fn(),
registerMountContext: jest.fn(),
});

const createMock = (): jest.Mocked<ApplicationServiceContract> => ({
Expand Down
122 changes: 95 additions & 27 deletions src/core/public/application/application_service.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,125 @@
* under the License.
*/

import React from 'react';
import { shallow } from 'enzyme';

import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock';
import { MockCapabilitiesService } from './application_service.test.mocks';
import { ApplicationService } from './application_service';
import { contextServiceMock } from '../context/context_service.mock';
import { httpServiceMock } from '../http/http_service.mock';
import { History } from 'history';

const coreContext = { coreId: Symbol() };

describe('#start()', () => {
it('exposes available apps from capabilities', async () => {
const service = new ApplicationService();
const setup = service.setup();
setup.registerApp({ id: 'app1' } as any);
const service = new ApplicationService(coreContext);
const context = contextServiceMock.createSetupContract();
const setup = service.setup({ context });
setup.registerApp(Symbol(), { id: 'app1' } as any);
setup.registerLegacyApp({ id: 'app2' } as any);

const http = httpServiceMock.createStartContract();
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
const startContract = await service.start({ injectedMetadata });
const startContract = await service.start({ http, injectedMetadata });

expect(startContract.availableApps).toMatchInlineSnapshot(`
Array [
Object {
"id": "app1",
},
]
`);
Map {
"app1" => Object {
"id": "app1",
"mountHandler": [Function],
},
}
`);
expect(startContract.availableLegacyApps).toMatchInlineSnapshot(`
Array [
Object {
"id": "app2",
},
]
`);
Map {
"app2" => Object {
"id": "app2",
},
}
`);
});

it('passes registered applications to capabilities', async () => {
const service = new ApplicationService();
const setup = service.setup();
setup.registerApp({ id: 'app1' } as any);
const service = new ApplicationService(coreContext);
const context = contextServiceMock.createSetupContract();
const setup = service.setup({ context });
setup.registerApp(Symbol(), { id: 'app1' } as any);

const http = httpServiceMock.createStartContract();
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
await service.start({ injectedMetadata });
await service.start({ http, injectedMetadata });

expect(MockCapabilitiesService.start).toHaveBeenCalledWith({
apps: [{ id: 'app1' }],
legacyApps: [],
apps: new Map([['app1', { id: 'app1', mountHandler: expect.any(Function) }]]),
legacyApps: new Map(),
injectedMetadata,
});
});

it('passes registered legacy applications to capabilities', async () => {
const service = new ApplicationService();
const setup = service.setup();
const service = new ApplicationService(coreContext);
const context = contextServiceMock.createSetupContract();
const setup = service.setup({ context });
setup.registerLegacyApp({ id: 'legacyApp1' } as any);

const http = httpServiceMock.createStartContract();
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
await service.start({ injectedMetadata });
await service.start({ http, injectedMetadata });

expect(MockCapabilitiesService.start).toHaveBeenCalledWith({
apps: [],
legacyApps: [{ id: 'legacyApp1' }],
apps: new Map(),
legacyApps: new Map([['legacyApp1', { id: 'legacyApp1' }]]),
injectedMetadata,
});
});

it('returns renderable JSX tree', async () => {
const service = new ApplicationService(coreContext);
const context = contextServiceMock.createSetupContract();
service.setup({ context });

const http = httpServiceMock.createStartContract();
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
injectedMetadata.getLegacyMode.mockReturnValue(false);
const start = await service.start({ http, injectedMetadata });

expect(shallow(React.createElement(() => start.getComponent()))).toMatchSnapshot();
});

describe('navigateToApp', () => {
it('changes the browser history to /app/:appId', async () => {
const service = new ApplicationService(coreContext);
const context = contextServiceMock.createSetupContract();
service.setup({ context });

const http = httpServiceMock.createStartContract();
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
injectedMetadata.getLegacyMode.mockReturnValue(false);
const start = await service.start({ http, injectedMetadata });

const history = start.getComponent()!.props.history as History<any>;
start.navigateToApp('myTestApp');
expect(history.location.pathname).toEqual('/app/myTestApp');
start.navigateToApp('myOtherApp');
expect(history.location.pathname).toEqual('/app/myOtherApp');
});

it('appends a path if specified', async () => {
const service = new ApplicationService(coreContext);
const context = contextServiceMock.createSetupContract();
service.setup({ context });

const http = httpServiceMock.createStartContract();
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
injectedMetadata.getLegacyMode.mockReturnValue(false);
const start = await service.start({ http, injectedMetadata });

const history = start.getComponent()!.props.history as History<any>;
start.navigateToApp('myTestApp', 'deep/link/to/location/2');
expect(history.location.pathname).toEqual('/app/myTestApp/deep/link/to/location/2');
});
});
});
Loading

0 comments on commit adcb6cc

Please sign in to comment.