-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ui/core/uiSettings] add tests for uiSettingsService
- Loading branch information
spalger
committed
Sep 5, 2018
1 parent
9618563
commit df345e1
Showing
4 changed files
with
205 additions
and
19 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/core/public/legacy_platform/__snapshots__/legacy_platform_service.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/core/public/ui_settings/__snapshots__/ui_settings_service.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`#start contstructs UiSettingsClient and UiSettingsApi: UiSettingsApi args 1`] = ` | ||
[MockFunction MockUiSettingsApi] { | ||
"calls": Array [ | ||
Array [ | ||
Object { | ||
"basePathStartContract": true, | ||
}, | ||
"kibanaVersion", | ||
], | ||
], | ||
} | ||
`; | ||
|
||
exports[`#start contstructs UiSettingsClient and UiSettingsApi: UiSettingsClient args 1`] = ` | ||
[MockFunction MockUiSettingsClient] { | ||
"calls": Array [ | ||
Array [ | ||
Object { | ||
"api": mockConstructor { | ||
"getLoadingCount$": [MockFunction] { | ||
"calls": Array [ | ||
Array [], | ||
], | ||
}, | ||
"stop": [MockFunction], | ||
}, | ||
"defaults": Object { | ||
"legacyInjectedUiSettingDefaults": true, | ||
}, | ||
"initialSettings": Object { | ||
"legacyInjectedUiSettingUserValues": true, | ||
}, | ||
"onUpdateError": [Function], | ||
}, | ||
], | ||
], | ||
} | ||
`; | ||
|
||
exports[`#start passes the uiSettings loading count to the loading count api: loadingCount.add calls 1`] = ` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
Object { | ||
"loadingCountObservable": true, | ||
}, | ||
], | ||
], | ||
} | ||
`; |
113 changes: 113 additions & 0 deletions
113
src/core/public/ui_settings/ui_settings_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
// Mock the UiSettingsApi class | ||
import { UiSettingsApi } from './ui_settings_api'; | ||
const MockUiSettingsApi = jest | ||
.fn<UiSettingsApi>(function(this: any) { | ||
this.stop = jest.fn(); | ||
this.getLoadingCount$ = jest.fn().mockReturnValue({ | ||
loadingCountObservable: true, | ||
}); | ||
}) | ||
.mockName('MockUiSettingsApi'); | ||
jest.mock('./ui_settings_api', () => ({ | ||
UiSettingsApi: MockUiSettingsApi, | ||
})); | ||
|
||
// Mock the UiSettingsClient class | ||
import { UiSettingsClient } from './ui_settings_client'; | ||
const MockUiSettingsClient = jest | ||
.fn<UiSettingsClient>(function(this: any) { | ||
this.stop = jest.fn(); | ||
}) | ||
.mockName('MockUiSettingsClient'); | ||
jest.mock('./ui_settings_client', () => ({ | ||
UiSettingsClient: MockUiSettingsClient, | ||
})); | ||
|
||
// Load the service | ||
import { UiSettingsService } from './ui_settings_service'; | ||
|
||
const loadingCountStartContract = { | ||
loadingCountStartContract: true, | ||
add: jest.fn(), | ||
}; | ||
|
||
const defaultDeps: any = { | ||
notifications: { | ||
notificationsStartContract: true, | ||
}, | ||
loadingCount: loadingCountStartContract, | ||
injectedMetadata: { | ||
injectedMetadataStartContract: true, | ||
getKibanaVersion: jest.fn().mockReturnValue('kibanaVersion'), | ||
getLegacyMetadata: jest.fn().mockReturnValue({ | ||
uiSettings: { | ||
defaults: { legacyInjectedUiSettingDefaults: true }, | ||
user: { legacyInjectedUiSettingUserValues: true }, | ||
}, | ||
}), | ||
}, | ||
basePath: { | ||
basePathStartContract: true, | ||
}, | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('#start', () => { | ||
it('returns an instance of UiSettingsClient', () => { | ||
const start = new UiSettingsService().start(defaultDeps); | ||
expect(start).toBeInstanceOf(MockUiSettingsClient); | ||
}); | ||
|
||
it('contstructs UiSettingsClient and UiSettingsApi', () => { | ||
new UiSettingsService().start(defaultDeps); | ||
|
||
expect(MockUiSettingsApi).toMatchSnapshot('UiSettingsApi args'); | ||
expect(MockUiSettingsClient).toMatchSnapshot('UiSettingsClient args'); | ||
}); | ||
|
||
it('passes the uiSettings loading count to the loading count api', () => { | ||
new UiSettingsService().start(defaultDeps); | ||
|
||
expect(loadingCountStartContract.add).toMatchSnapshot('loadingCount.add calls'); | ||
}); | ||
}); | ||
|
||
describe('#stop', () => { | ||
it('runs fine if service never started', () => { | ||
const service = new UiSettingsService(); | ||
expect(() => service.stop()).not.toThrowError(); | ||
}); | ||
|
||
it('stops the uiSettingsClient and uiSettingsApi', () => { | ||
const service = new UiSettingsService(); | ||
const client = service.start(defaultDeps); | ||
const [[{ api }]] = MockUiSettingsClient.mock.calls; | ||
jest.spyOn(client, 'stop'); | ||
jest.spyOn(api, 'stop'); | ||
service.stop(); | ||
expect(api.stop).toHaveBeenCalledTimes(1); | ||
expect(client.stop).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |