Skip to content

Commit

Permalink
feat: #87 - fix search - add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
eouin committed Jun 8, 2023
1 parent f662053 commit c92a4ea
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 27 deletions.
24 changes: 23 additions & 1 deletion packages/webapp/src/webview/features/app/App.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,29 @@ export const initialAppState: AppState = {

export const initialUiState: AppUiState = {
ready: false,
tabs: {}
tabs: {
home: {
key: PathType.HOME,
path: '/',
headerText: '',
ariaLabel: 'HOME_TAB',
count: -1
},
tutorials: {
key: PathType.TUTORIALS,
path: '/tutorials',
headerText: '',
ariaLabel: 'TUTORIALS_TAB',
count: -1
},
blogs: {
key: PathType.BLOGS,
path: '/blogs',
headerText: '',
ariaLabel: 'BLOGS_TAB',
count: -1
}
}
};

const app = createSlice({
Expand Down
139 changes: 119 additions & 20 deletions packages/webapp/test/__mocks__/app.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,135 @@
import { App } from '@sap/knowledge-hub-extension-types';

export const appStateTabsBlogs = {
ariaLabel: 'BLOGS_TAB',
count: -1,
headerText: '',
key: 'blogs',
path: '/blogs'
};

export const appStateTabsBlogsWithCount = {
ariaLabel: 'BLOGS_TAB',
count: 10,
headerText: '',
key: 'blogs',
path: '/blogs'
};

export const appStateTabsHome = {
ariaLabel: 'HOME_TAB',
count: -1,
headerText: '',
key: 'home',
path: '/'
};

export const appStateTabsTutorials = {
ariaLabel: 'TUTORIALS_TAB',
count: -1,
headerText: '',
key: 'tutorials',
path: '/tutorials'
};

export const appStateTabsTutorialsWithCount = {
ariaLabel: 'TUTORIALS_TAB',
count: 10,
headerText: '',
key: 'tutorials',
path: '/tutorials'
};

export const appStateTabsBlogsWithText = {
ariaLabel: 'BLOGS_TAB',
count: -1,
headerText: 'BLOGS_TAB',
key: 'blogs',
path: '/blogs'
};

export const appStateTabsHomeWithText = {
ariaLabel: 'HOME_TAB',
count: -1,
headerText: 'HOME_TAB',
key: 'home',
path: '/'
};

export const appStateTabsTutorialsWithText = {
ariaLabel: 'TUTORIALS_TAB',
count: -1,
headerText: 'TUTORIALS_TAB',
key: 'tutorials',
path: '/tutorials'
};

export const appStateTabs = {
blogs: appStateTabsBlogs,
home: appStateTabsHome,
tutorials: appStateTabsTutorials
};

export const appStateTabsWithText = {
blogs: appStateTabsBlogsWithText,
home: appStateTabsHomeWithText,
tutorials: appStateTabsTutorialsWithText
};

export const appStateTabsWithBlogsCount = {
blogs: appStateTabsBlogsWithCount,
home: appStateTabsHome,
tutorials: appStateTabsTutorials
};

export const appStateTabsWithTutorialsCount = {
blogs: appStateTabsBlogs,
home: appStateTabsHome,
tutorials: appStateTabsTutorialsWithCount
};

//

export const appInitialState: App = {
app: {
appFilters: {},
appId: ''
},
ui: {
ready: false,
tabs: {}
tabs: appStateTabs
}
};

export const appStateTabs = {
blogs: {
ariaLabel: 'BLOGS_TAB',
count: -1,
headerText: '',
key: 'blogs',
path: '/blogs'
export const appInitialStateReady: App = {
app: {
appFilters: {},
appId: ''
},
ui: {
ready: true,
tabs: appStateTabsWithText
}
};

export const appStateWithTabsWithBlogsCount = {
app: {
appFilters: {},
appId: ''
},
home: {
ariaLabel: 'HOME_TAB',
count: -1,
headerText: '',
key: 'home',
path: '/'
ui: {
ready: false,
tabs: appStateTabsWithBlogsCount
}
};

export const appStateWithTabsWithTutorialsCount = {
app: {
appFilters: {},
appId: ''
},
tutorials: {
ariaLabel: 'TUTORIALS_TAB',
count: -1,
headerText: '',
key: 'tutorials',
path: '/tutorials'
ui: {
ready: false,
tabs: appStateTabsWithTutorialsCount
}
};
55 changes: 49 additions & 6 deletions packages/webapp/test/features/App/App.slice.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { initialize } from '@sap/knowledge-hub-extension-types';
import reducer from '../../../src/webview/features/app/App.slice';

import { appInitialState } from '../../__mocks__/app';
import { initialize, fetchBlogsTotalCount, fetchTutorialsTotalCount } from '@sap/knowledge-hub-extension-types';
import reducer, { getAppId, getAppReady, getAppTabs } from '../../../src/webview/features/app/App.slice';
import { appBlogsTotalCountUpdate, appTutorialsTotalCountUpdate } from '../../../src/webview/store/actions';
import {
appInitialState,
appInitialStateReady,
appStateWithTabsWithBlogsCount,
appStateWithTabsWithTutorialsCount
} from '../../__mocks__/app';
import { rootState } from '../../../test/__mocks__/store.mock';

describe('app slice', () => {
describe('actions', () => {
describe('app slice > actions', () => {
test('initialize', () => {
const expectedAction = {
type: '[core] app/initialize <fulfilled>',
Expand All @@ -18,9 +24,46 @@ describe('app slice', () => {
});
});

describe('reducer', () => {
describe('app slice > reducer', () => {
test('initial state', () => {
expect(reducer(undefined, { type: 'action' })).toEqual(appInitialState);
});

test('initialize action', () => {
const action = initialize.fulfilled({ appId: '', appFilters: {} });
expect(reducer(undefined, action)).toEqual(appInitialStateReady);
});

test('fetchBlogsTotalCount fulfilled action', () => {
const action = fetchBlogsTotalCount.fulfilled(10);
expect(reducer(undefined, action)).toEqual(appStateWithTabsWithBlogsCount);
});

test('fetchTutorialsTotalCount fulfilled action', () => {
const action = fetchTutorialsTotalCount.fulfilled(10);
expect(reducer(undefined, action)).toEqual(appStateWithTabsWithTutorialsCount);
});

test('appBlogsTotalCountUpdate fulfilled action', () => {
const action = appBlogsTotalCountUpdate(10);
expect(reducer(undefined, action)).toEqual(appStateWithTabsWithBlogsCount);
});

test('appTutorialsTotalCountUpdate fulfilled action', () => {
const action = appTutorialsTotalCountUpdate(10);
expect(reducer(undefined, action)).toEqual(appStateWithTabsWithTutorialsCount);
});
});

describe('app slice > state selector', () => {
test('app slice > state selector > getAppId', () => {
expect(getAppId(rootState)).toEqual(appInitialState.app.appId);
});
test('app slice > state selector > getAppReady', () => {
expect(getAppReady(rootState)).toEqual(appInitialState.ui.ready);
});
test('app slice > state selector > getAppTabs', () => {
expect(getAppTabs(rootState)).toEqual(appInitialState.ui.tabs);
});
});
});

0 comments on commit c92a4ea

Please sign in to comment.