Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Jan 12, 2021
1 parent 0816148 commit 0b24d18
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 58 deletions.
64 changes: 11 additions & 53 deletions src/plugins/data/public/search/search_interceptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ describe('SearchInterceptor', () => {
sessionId?: string;
}) => {
const sessionServiceMock = searchMock.session as jest.Mocked<ISessionService>;
sessionServiceMock.getSessionId.mockImplementation(() => sessionId);
sessionServiceMock.isRestore.mockImplementation(() => isRestore);
sessionServiceMock.isStored.mockImplementation(() => isStored);
sessionServiceMock.getSearchOptions.mockImplementation(() => ({
sessionId,
isRestore,
isStored,
}));
fetchMock.mockResolvedValue({ result: 200 });
};

Expand All @@ -130,72 +132,28 @@ describe('SearchInterceptor', () => {

afterEach(() => {
const sessionServiceMock = searchMock.session as jest.Mocked<ISessionService>;
sessionServiceMock.getSessionId.mockReset();
sessionServiceMock.isRestore.mockReset();
sessionServiceMock.isStored.mockReset();
sessionServiceMock.getSearchOptions.mockReset();
fetchMock.mockReset();
});

test('infers isRestore from session service state', async () => {
test('gets session search options from session service', async () => {
const sessionId = 'sid';
setup({
isRestore: true,
sessionId,
});

await searchInterceptor.search(mockRequest, { sessionId }).toPromise();
expect(fetchMock.mock.calls[0][0]).toEqual(
expect.objectContaining({
options: { sessionId: 'sid', isStored: false, isRestore: true },
})
);
});

test('infers isStored from session service state', async () => {
const sessionId = 'sid';
setup({
isStored: true,
sessionId,
});

await searchInterceptor.search(mockRequest, { sessionId }).toPromise();
expect(fetchMock.mock.calls[0][0]).toEqual(
expect.objectContaining({
options: { sessionId: 'sid', isStored: true, isRestore: false },
})
);
});

test('skips isRestore & isStore in case not a current session Id', async () => {
setup({
isStored: true,
isRestore: true,
sessionId: 'session id',
});

await searchInterceptor
.search(mockRequest, { sessionId: 'different session id' })
.toPromise();
expect(fetchMock.mock.calls[0][0]).toEqual(
expect.objectContaining({
options: { sessionId: 'different session id', isStored: false, isRestore: false },
options: { sessionId, isStored: true, isRestore: true },
})
);
});

test('skips isRestore & isStore in case no session Id', async () => {
setup({
isStored: true,
isRestore: true,
sessionId: undefined,
});

await searchInterceptor.search(mockRequest, { sessionId: 'sessionId' }).toPromise();
expect(fetchMock.mock.calls[0][0]).toEqual(
expect.objectContaining({
options: { sessionId: 'sessionId', isStored: false, isRestore: false },
})
);
expect(
(searchMock.session as jest.Mocked<ISessionService>).getSearchOptions
).toHaveBeenCalledWith(sessionId);
});
});

Expand Down
85 changes: 84 additions & 1 deletion src/plugins/data/public/search/session/session_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@ describe('Session service', () => {

beforeEach(() => {
const initializerContext = coreMock.createPluginInitializerContext();
const startService = coreMock.createSetup().getStartServices;
sessionService = new SessionService(
initializerContext,
coreMock.createSetup().getStartServices,
() =>
startService().then(([coreStart, ...rest]) => [
{
...{
...coreStart,
application: { ...coreStart.application, currentAppId$: new BehaviorSubject('app') },
},
},
...rest,
]),
getSessionsClientMock(),
{ freezeState: false } // needed to use mocks inside state container
);
Expand Down Expand Up @@ -93,4 +103,77 @@ describe('Session service', () => {
expect(abort).toBeCalledTimes(3);
});
});

test('getSearchOptions infers isRestore & isStored from state', async () => {
const sessionId = sessionService.start();
const someOtherId = 'some-other-id';
expect(sessionService.getSearchOptions()).toEqual({
isStored: false,
isRestore: false,
sessionId: undefined,
});
expect(sessionService.getSearchOptions(someOtherId)).toEqual({
isStored: false,
isRestore: false,
sessionId: someOtherId,
});
expect(sessionService.getSearchOptions(sessionId)).toEqual({
isStored: false,
isRestore: false,
sessionId,
});

sessionService.setSearchSessionInfoProvider({
getName: async () => 'Name',
getUrlGeneratorData: async () => ({
urlGeneratorId: 'id',
initialState: {},
restoreState: {},
}),
});
await sessionService.save();

expect(sessionService.getSearchOptions()).toEqual({
isStored: false,
isRestore: false,
sessionId: undefined,
});
expect(sessionService.getSearchOptions(someOtherId)).toEqual({
isStored: false,
isRestore: false,
sessionId: someOtherId,
});
expect(sessionService.getSearchOptions(sessionId)).toEqual({
isStored: true,
isRestore: false,
sessionId,
});

await sessionService.restore(sessionId);

expect(sessionService.getSearchOptions()).toEqual({
isStored: false,
isRestore: false,
sessionId: undefined,
});
expect(sessionService.getSearchOptions(someOtherId)).toEqual({
isStored: false,
isRestore: false,
sessionId: someOtherId,
});
expect(sessionService.getSearchOptions(sessionId)).toEqual({
isStored: true,
isRestore: true,
sessionId,
});
});
test('isCurrentSession', () => {
expect(sessionService.isCurrentSession()).toBeFalsy();

const sessionId = sessionService.start();

expect(sessionService.isCurrentSession()).toBeFalsy();
expect(sessionService.isCurrentSession('some-other')).toBeFalsy();
expect(sessionService.isCurrentSession(sessionId)).toBeTruthy();
});
});
6 changes: 3 additions & 3 deletions src/plugins/data/public/search/session/session_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,11 @@ export class SessionService {
public getSearchOptions(
sessionId?: string
): Pick<ISearchOptions, 'sessionId' | 'isRestore' | 'isStored'> {
const currentSessionId = this.getSessionId();
const isCurrentSession = this.isCurrentSession(sessionId);
return {
sessionId,
isRestore: currentSessionId ? this.isRestore() : false,
isStored: currentSessionId ? this.isStored() : false,
isRestore: isCurrentSession ? this.isRestore() : false,
isStored: isCurrentSession ? this.isStored() : false,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ describe('EnhancedSearchInterceptor', () => {

test('should NOT DELETE a running SAVED async search on abort', async () => {
const sessionId = 'sessionId';
sessionService.getSessionId.mockImplementation(() => sessionId);
sessionService.isCurrentSession.mockImplementation((_sessionId) => _sessionId === sessionId);
const responses = [
{
time: 10,
Expand Down Expand Up @@ -479,6 +479,7 @@ describe('EnhancedSearchInterceptor', () => {

test('should track searches', async () => {
const sessionId = 'sessionId';
sessionService.isCurrentSession.mockImplementation((_sessionId) => _sessionId === sessionId);
sessionService.getSessionId.mockImplementation(() => sessionId);

const untrack = jest.fn();
Expand All @@ -496,6 +497,7 @@ describe('EnhancedSearchInterceptor', () => {

test('session service should be able to cancel search', async () => {
const sessionId = 'sessionId';
sessionService.isCurrentSession.mockImplementation((_sessionId) => _sessionId === sessionId);
sessionService.getSessionId.mockImplementation(() => sessionId);

const untrack = jest.fn();
Expand All @@ -519,6 +521,7 @@ describe('EnhancedSearchInterceptor', () => {

test("don't track non current session searches", async () => {
const sessionId = 'sessionId';
sessionService.isCurrentSession.mockImplementation((_sessionId) => _sessionId === sessionId);
sessionService.getSessionId.mockImplementation(() => sessionId);

const untrack = jest.fn();
Expand All @@ -539,6 +542,7 @@ describe('EnhancedSearchInterceptor', () => {

test("don't track if no current session", async () => {
sessionService.getSessionId.mockImplementation(() => undefined);
sessionService.isCurrentSession.mockImplementation((_sessionId) => false);

const untrack = jest.fn();
sessionService.trackSearch.mockImplementation(() => untrack);
Expand Down

0 comments on commit 0b24d18

Please sign in to comment.