-
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
[Endpoint] Host Details Policy Response Panel #63518
Merged
paul-tavares
merged 17 commits into
elastic:master
from
paul-tavares:task/EMT-163-policy-Response-panel
Apr 16, 2020
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9950f9b
File/code restructure + policy response component placeholder
paul-tavares a79bc42
Added link to Policy status that updates URL and show details panel
paul-tavares a6ea3fa
Policy Response panel title + back to details link
paul-tavares 9e6c3f5
Move sub-panel title to EuiFlyoutHeader
paul-tavares ed9f2c5
label change
paul-tavares 8b5182d
Custom Styled Flyout Panel sub-header component
paul-tavares 72e1c5e
Fix type
paul-tavares 0c9df52
Move Middleware spy utils under `store/` for re-use
paul-tavares f00bcbb
Changed `appStoreFactory()` to accept optional `additionalMiddleware`…
paul-tavares 9b45f61
`waitForAction` middleware test utility now return Action on Promise …
paul-tavares 11e6f0c
Test cases for Host Details Policy Response panel
paul-tavares 135e014
Fix Functional test case
paul-tavares 469655f
Merge remote-tracking branch 'upstream/master' into task/EMT-163-poli…
paul-tavares a29a22f
Fixes post-master merge
paul-tavares 9069a40
Simplified the types for `waitForAction`
paul-tavares 9bfbb22
Updated PageView component to remove bottom margin
paul-tavares c99b360
Merge remote-tracking branch 'upstream/master' into task/EMT-163-poli…
paul-tavares 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
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
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
126 changes: 126 additions & 0 deletions
126
x-pack/plugins/endpoint/public/applications/endpoint/store/test_utils.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,126 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { Dispatch } from 'redux'; | ||
import { AppAction, GlobalState, MiddlewareFactory } from '../types'; | ||
|
||
/** | ||
* Utilities for testing Redux middleware | ||
*/ | ||
export interface MiddlewareActionSpyHelper<S = GlobalState, A extends AppAction = AppAction> { | ||
/** | ||
* Returns a promise that is fulfilled when the given action is dispatched or a timeout occurs. | ||
* The `action` will given to the promise `resolve` thus allowing for checks to be done. | ||
* The use of this method instead of a `sleep()` type of delay should avoid test case instability | ||
* especially when run in a CI environment. | ||
* | ||
* @param actionType | ||
*/ | ||
waitForAction: <T extends A['type']>(actionType: T) => Promise<A extends { type: T } ? A : never>; | ||
/** | ||
* A property holding the information around the calls that were processed by the internal | ||
* `actionSpyMiddelware`. This property holds the information typically found in Jets's mocked | ||
* function `mock` property - [see here for more information](https://jestjs.io/docs/en/mock-functions#mock-property) | ||
* | ||
* **Note**: this property will only be set **after* the `actionSpyMiddlware` has been | ||
* initialized (ex. via `createStore()`. Attempting to reference this property before that time | ||
* will throw an error. | ||
* Also - do not hold on to references to this property value if `jest.clearAllMocks()` or | ||
* `jest.resetAllMocks()` is called between usages of the value. | ||
*/ | ||
dispatchSpy: jest.Mock<Dispatch<A>>['mock']; | ||
/** | ||
* Redux middleware that enables spying on the action that are dispatched through the store | ||
*/ | ||
actionSpyMiddleware: ReturnType<MiddlewareFactory<S>>; | ||
} | ||
|
||
/** | ||
* Creates a new instance of middleware action helpers | ||
* Note: in most cases (testing concern specific middleware) this function should be given | ||
* the state type definition, else, the global state will be used. | ||
* | ||
* @example | ||
* // Use in Policy List middleware testing | ||
* const middlewareSpyUtils = createSpyMiddleware<PolicyListState>(); | ||
* store = createStore( | ||
* policyListReducer, | ||
* applyMiddleware( | ||
* policyListMiddlewareFactory(fakeCoreStart, depsStart), | ||
* middlewareSpyUtils.actionSpyMiddleware | ||
* ) | ||
* ); | ||
* // Reference `dispatchSpy` ONLY after creating the store that includes `actionSpyMiddleware` | ||
* const { waitForAction, dispatchSpy } = middlewareSpyUtils; | ||
* // | ||
* // later in test | ||
* // | ||
* it('...', async () => { | ||
* //... | ||
* await waitForAction('serverReturnedPolicyListData'); | ||
* // do assertions | ||
* // or check how action was called | ||
* expect(dispatchSpy.calls.length).toBe(2) | ||
* }); | ||
*/ | ||
export const createSpyMiddleware = < | ||
S = GlobalState, | ||
A extends AppAction = AppAction | ||
>(): MiddlewareActionSpyHelper<S, A> => { | ||
type ActionWatcher = (action: A) => void; | ||
|
||
const watchers = new Set<ActionWatcher>(); | ||
let spyDispatch: jest.Mock<Dispatch<A>>; | ||
|
||
return { | ||
waitForAction: async actionType => { | ||
type ResolvedAction = A extends { type: typeof actionType } ? A : never; | ||
|
||
// Error is defined here so that we get a better stack trace that points to the test from where it was used | ||
const err = new Error(`action '${actionType}' was not dispatched within the allocated time`); | ||
|
||
return new Promise<ResolvedAction>((resolve, reject) => { | ||
const watch: ActionWatcher = action => { | ||
if (action.type === actionType) { | ||
watchers.delete(watch); | ||
clearTimeout(timeout); | ||
resolve(action as ResolvedAction); | ||
} | ||
}; | ||
|
||
// We timeout before jest's default 5s, so that a better error stack is returned | ||
const timeout = setTimeout(() => { | ||
watchers.delete(watch); | ||
reject(err); | ||
}, 4500); | ||
watchers.add(watch); | ||
}); | ||
}, | ||
|
||
get dispatchSpy() { | ||
if (!spyDispatch) { | ||
throw new Error( | ||
'Spy Middleware has not been initialized. Access this property only after using `actionSpyMiddleware` in a redux store' | ||
); | ||
} | ||
return spyDispatch.mock; | ||
}, | ||
|
||
actionSpyMiddleware: api => { | ||
return next => { | ||
spyDispatch = jest.fn(action => { | ||
next(action); | ||
// loop through the list of watcher (if any) and call them with this action | ||
for (const watch of watchers) { | ||
watch(action); | ||
} | ||
return action; | ||
}); | ||
return spyDispatch; | ||
}; | ||
}, | ||
}; | ||
}; |
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.
@oatkiller could you review and comment on this change?
This goes along with a change to the
appStoreMiddleware()
(see further below). It enables us to inject theactionSpyMiddleware
into the application store for testing purposes.Let me know your thoughts
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 generally think this makes sense. One thought though:
When looking at the
appStoreFactory
, I see these comments:Based on those, could we replace:
with:
Let me know your thoughts
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.
Thanks for the comments. Yeah, that makes sense. I assume that would be (from our
types.ts
) -ReturnType<MiddlewareFactory>
so that it is correctly typed for the dispatch signature.