forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add plugin test for sync/error cases #7
Merged
tsullivan
merged 2 commits into
tsullivan:reporting/setup-nsyncified
from
joelgriffith:reporting/setup-nsyncified
Jun 9, 2020
+136
−20
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,103 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
jest.mock('./browsers/install', () => ({ | ||
installBrowser: jest.fn().mockImplementation(() => ({ | ||
binaryPath$: { | ||
pipe: jest.fn().mockImplementation(() => ({ | ||
toPromise: () => Promise.resolve(), | ||
})), | ||
}, | ||
})), | ||
})); | ||
|
||
import { coreMock } from 'src/core/server/mocks'; | ||
import { ReportingPlugin } from './plugin'; | ||
import { createMockConfig } from './test_helpers'; | ||
|
||
const sleep = (time: number) => new Promise((r) => setTimeout(r, time)); | ||
|
||
describe('Reporting Plugin', () => { | ||
let config: any; | ||
let initContext: any; | ||
let coreSetup: any; | ||
let coreStart: any; | ||
let pluginSetup: any; | ||
let pluginStart: any; | ||
|
||
beforeEach(async () => { | ||
config = createMockConfig(); | ||
initContext = coreMock.createPluginInitializerContext(config); | ||
coreSetup = await coreMock.createSetup(config); | ||
coreStart = await coreMock.createStart(); | ||
pluginSetup = ({ | ||
licensing: {}, | ||
usageCollection: { | ||
makeUsageCollector: jest.fn(), | ||
registerCollector: jest.fn(), | ||
}, | ||
security: { | ||
authc: { | ||
getCurrentUser: () => ({ | ||
id: '123', | ||
roles: ['superuser'], | ||
username: 'Tom Riddle', | ||
}), | ||
}, | ||
}, | ||
} as unknown) as any; | ||
pluginStart = ({ | ||
data: { | ||
fieldFormats: {}, | ||
}, | ||
} as unknown) as any; | ||
}); | ||
|
||
it('has a sync setup process', () => { | ||
const plugin = new ReportingPlugin(initContext); | ||
|
||
expect(plugin.setup(coreSetup, pluginSetup)).not.toHaveProperty('then'); | ||
}); | ||
|
||
it('logs setup issues', async () => { | ||
const plugin = new ReportingPlugin(initContext); | ||
// @ts-ignore overloading error logger | ||
plugin.logger.error = jest.fn(); | ||
coreSetup.elasticsearch = null; | ||
plugin.setup(coreSetup, pluginSetup); | ||
|
||
await sleep(5); | ||
|
||
// @ts-ignore overloading error logger | ||
expect(plugin.logger.error.mock.calls[0][0]).toMatch( | ||
/Error in Reporting setup, reporting may not function properly/ | ||
); | ||
// @ts-ignore overloading error logger | ||
expect(plugin.logger.error).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('has a sync startup process', async () => { | ||
const plugin = new ReportingPlugin(initContext); | ||
plugin.setup(coreSetup, pluginSetup); | ||
await sleep(5); | ||
expect(plugin.start(coreStart, pluginStart)).not.toHaveProperty('then'); | ||
}); | ||
|
||
it('logs start issues', async () => { | ||
const plugin = new ReportingPlugin(initContext); | ||
// @ts-ignore overloading error logger | ||
plugin.logger.error = jest.fn(); | ||
plugin.setup(coreSetup, pluginSetup); | ||
await sleep(5); | ||
plugin.start(null as any, pluginStart); | ||
await sleep(10); | ||
// @ts-ignore overloading error logger | ||
expect(plugin.logger.error.mock.calls[0][0]).toMatch( | ||
/Error in Reporting start, reporting may not function properly/ | ||
); | ||
// @ts-ignore overloading error logger | ||
expect(plugin.logger.error).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
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
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 |
---|---|---|
|
@@ -36,30 +36,35 @@ const createMockSetupDeps = (setupMock?: any): ReportingSetupDeps => { | |
licensing: { | ||
license$: of({ isAvailable: true, isActive: true, type: 'basic' }), | ||
} as any, | ||
usageCollection: {} as any, | ||
usageCollection: { | ||
makeUsageCollector: jest.fn(), | ||
registerCollector: jest.fn(), | ||
} as any, | ||
}; | ||
}; | ||
|
||
export const createMockConfig = (overrides?: any) => ({ | ||
index: '.reporting', | ||
kibanaServer: { | ||
hostname: 'localhost', | ||
port: '80', | ||
}, | ||
capture: { | ||
browser: { | ||
chromium: { | ||
disableSandbox: true, | ||
}, | ||
}, | ||
}, | ||
...overrides, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: since this doesn't return a |
||
|
||
export const createMockStartDeps = (startMock?: any): ReportingStartDeps => ({ | ||
data: startMock.data, | ||
}); | ||
|
||
const createMockReportingPlugin = async (config: ReportingConfig): Promise<ReportingPlugin> => { | ||
const mockConfig = { | ||
index: '.reporting', | ||
kibanaServer: { | ||
hostname: 'localhost', | ||
port: '80', | ||
}, | ||
capture: { | ||
browser: { | ||
chromium: { | ||
disableSandbox: true, | ||
}, | ||
}, | ||
}, | ||
...config, | ||
}; | ||
const mockConfig = createMockConfig(config); | ||
const plugin = new ReportingPlugin(coreMock.createPluginInitializerContext(mockConfig)); | ||
const setupMock = coreMock.createSetup(); | ||
const coreStartMock = coreMock.createStart(); | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This felt better/easier to write more async setup tasks with in the future since we have our own .catch in case issues should arise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it!