-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 client side service mocks #32999
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
027d812
Add client side service mocks
mshustov 7e58111
Merge branch 'master' into issue-31486-client-mocks
mshustov c77a59d
remove ab obsolete test snapshot
mshustov 7be3421
put back accidentally removed type definition
mshustov e517aa6
Merge branch 'master' into issue-31486-client-mocks
mshustov df95b2a
define MethodKeysOf, PublicMethodsOf on project level
mshustov 1022fae
export src/core/public service mocks
mshustov f6433b5
export src/core/server service mocks
mshustov 314eded
Merge branch 'master' into issue-31486-client-mocks
mshustov 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,51 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import { BasePathService, BasePathStart } from './base_path_service'; | ||
|
||
const createStartContractMock = () => { | ||
const startContract: jest.Mocked<BasePathStart> = { | ||
get: jest.fn(), | ||
addToPath: jest.fn(), | ||
removeFromPath: jest.fn(), | ||
}; | ||
startContract.get.mockReturnValue('get'); | ||
startContract.addToPath.mockReturnValue('addToPath'); | ||
startContract.removeFromPath.mockReturnValue('removeFromPath'); | ||
return startContract; | ||
}; | ||
|
||
type MethodKeysOf<T> = { | ||
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never | ||
}[keyof T]; | ||
|
||
type PublicMethodsOf<T> = Pick<T, MethodKeysOf<T>>; | ||
|
||
type BasePathServiceContract = PublicMethodsOf<BasePathService>; | ||
const createMock = () => { | ||
const mocked: jest.Mocked<BasePathServiceContract> = { | ||
start: jest.fn(), | ||
}; | ||
mocked.start.mockReturnValue(createStartContractMock()); | ||
return mocked; | ||
}; | ||
|
||
export const basePathServiceMock = { | ||
create: createMock, | ||
createStartContract: createStartContractMock, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { Brand, Breadcrumb, ChromeService, ChromeStart } from './chrome_service'; | ||
|
||
const createStartContractMock = () => { | ||
const startContract: jest.Mocked<ChromeStart> = { | ||
setBrand: jest.fn(), | ||
getBrand$: jest.fn(), | ||
setIsVisible: jest.fn(), | ||
getIsVisible$: jest.fn(), | ||
setIsCollapsed: jest.fn(), | ||
getIsCollapsed$: jest.fn(), | ||
addApplicationClass: jest.fn(), | ||
removeApplicationClass: jest.fn(), | ||
getApplicationClasses$: jest.fn(), | ||
getBreadcrumbs$: jest.fn(), | ||
setBreadcrumbs: jest.fn(), | ||
getHelpExtension$: jest.fn(), | ||
setHelpExtension: jest.fn(), | ||
}; | ||
startContract.getBrand$.mockReturnValue(new BehaviorSubject({} as Brand)); | ||
startContract.getIsVisible$.mockReturnValue(new BehaviorSubject(false)); | ||
startContract.getIsCollapsed$.mockReturnValue(new BehaviorSubject(false)); | ||
startContract.getApplicationClasses$.mockReturnValue(new BehaviorSubject(['class-name'])); | ||
startContract.getBreadcrumbs$.mockReturnValue(new BehaviorSubject([{} as Breadcrumb])); | ||
startContract.getHelpExtension$.mockReturnValue(new BehaviorSubject(undefined)); | ||
return startContract; | ||
}; | ||
|
||
type MethodKeysOf<T> = { | ||
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never | ||
}[keyof T]; | ||
|
||
type PublicMethodsOf<T> = Pick<T, MethodKeysOf<T>>; | ||
|
||
type ChromeServiceContract = PublicMethodsOf<ChromeService>; | ||
const createMock = () => { | ||
const mocked: jest.Mocked<ChromeServiceContract> = { | ||
start: jest.fn(), | ||
stop: jest.fn(), | ||
}; | ||
mocked.start.mockReturnValue(createStartContractMock()); | ||
return mocked; | ||
}; | ||
|
||
export const chromeServiceMock = { | ||
create: createMock, | ||
createStartContract: createStartContractMock, | ||
}; |
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
Oops, something went wrong.
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.
as this type definition is duplicated across all
*.mock.ts
files I'd suggest to finally decide where to can place such helpers. We can do that in following PRs thoThere 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'm definitely pro adding a file of "meta" types that are types used to create other types. This shouldn't have any types that are Kibana or even Node/Browser specific, but just generic types like this.
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 don't want to introduce new entities in the codebase. how about to just place such helpers in
typings/index.d.ts
? @joshdover @epixaThere 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.
That seems like an appropriate place to me.