-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03798c7
commit e6ee039
Showing
1 changed file
with
111 additions
and
0 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
x-pack/plugins/endpoint/public/applications/endpoint/store/endpoint_list/saga.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,111 @@ | ||
/* | ||
* 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 { CoreStart, HttpSetup } from 'kibana/public'; | ||
import { applyMiddleware, createStore, Dispatch, Store } from 'redux'; | ||
import { createSagaMiddleware, SagaContext } from '../../lib'; | ||
import { endpointListSaga } from './saga'; | ||
import { coreMock } from '../../../../../../../../src/core/public/mocks'; | ||
import { | ||
EndpointData, | ||
EndpointListAction, | ||
EndpointListData, | ||
endpointListReducer, | ||
EndpointListState, | ||
} from './index'; | ||
import { endpointListData } from './selectors'; | ||
|
||
describe('endpoint list saga', () => { | ||
const sleep = (ms = 100) => new Promise(wakeup => setTimeout(wakeup, ms)); | ||
let fakeCoreStart: jest.Mocked<CoreStart>; | ||
let fakeHttpServices: jest.Mocked<HttpSetup>; | ||
let store: Store<EndpointListState>; | ||
let dispatch: Dispatch<EndpointListAction>; | ||
|
||
// TODO: consolidate the below ++ helpers in `index.test.ts` into a `test_helpers.ts`?? | ||
const generateEndpoint = (): EndpointData => { | ||
return { | ||
machine_id: Math.random() | ||
.toString(16) | ||
.substr(2), | ||
created_at: new Date(), | ||
host: { | ||
name: '', | ||
hostname: '', | ||
ip: '', | ||
mac_address: '', | ||
os: { | ||
name: '', | ||
full: '', | ||
}, | ||
}, | ||
endpoint: { | ||
domain: '', | ||
is_base_image: true, | ||
active_directory_distinguished_name: '', | ||
active_directory_hostname: '', | ||
upgrade: { | ||
status: '', | ||
updated_at: new Date(), | ||
}, | ||
isolation: { | ||
status: false, | ||
request_status: true, | ||
updated_at: new Date(), | ||
}, | ||
policy: { | ||
name: '', | ||
id: '', | ||
}, | ||
sensor: { | ||
persistence: true, | ||
status: {}, | ||
}, | ||
}, | ||
}; | ||
}; | ||
const getEndpointListApiResponse = (): EndpointListData => { | ||
return { | ||
endpoints: [generateEndpoint()], | ||
request_page_size: 1, | ||
request_index: 1, | ||
total: 10, | ||
}; | ||
}; | ||
|
||
const endpointListSagaFactory = () => { | ||
return async (sagaContext: SagaContext) => { | ||
await endpointListSaga(sagaContext, fakeCoreStart).catch((e: Error) => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
return Promise.reject(e); | ||
}); | ||
}; | ||
}; | ||
|
||
beforeEach(() => { | ||
fakeCoreStart = coreMock.createStart({ basePath: '/mock' }); | ||
fakeHttpServices = fakeCoreStart.http as jest.Mocked<HttpSetup>; | ||
store = createStore( | ||
endpointListReducer, | ||
applyMiddleware(createSagaMiddleware(endpointListSagaFactory())) | ||
); | ||
dispatch = store.dispatch; | ||
}); | ||
|
||
test('it handles `userEnteredEndpointListPage`', async () => { | ||
const apiResponse = getEndpointListApiResponse(); | ||
|
||
fakeHttpServices.post.mockResolvedValue(apiResponse); | ||
expect(fakeHttpServices.post).not.toHaveBeenCalled(); | ||
|
||
dispatch({ type: 'userEnteredEndpointListPage' }); | ||
await sleep(); | ||
|
||
expect(fakeHttpServices.post).toHaveBeenCalledWith('/api/endpoint/endpoints'); | ||
expect(endpointListData(store.getState())).toEqual(apiResponse.endpoints); | ||
}); | ||
}); |