Skip to content

Commit

Permalink
feat: #87 - fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
eouin committed Jun 1, 2023
1 parent 5fbcec3 commit b8b07d7
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 263 deletions.
141 changes: 127 additions & 14 deletions packages/core/test/api/tags-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import axios from 'axios';
import type { TagsSearchResult } from '@sap/knowledge-hub-extension-types';
import { getCommunityTagsApi, getTags } from '../../src/api/tags-api';
import type { BlogsTagsSearchResult, TutorialsSearchResult } from '@sap/knowledge-hub-extension-types';
import { getCommunityTagsApi, getBlogsTags, getTutorialsTags } from '../../src/api/tags-api';

jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;

describe('tags-api', () => {
describe('getCommunityTagsApi', () => {
it('should test `getCommunityTagsApi` function', async () => {
const options = {
apiHost: 'https://searchproxy.api.community.sap.com'
};
const res = getCommunityTagsApi(options);

await expect(res).toBeDefined();
it('should test `getCommunityTagsApi` function', () => {
const res = getCommunityTagsApi();
expect(res).toBeDefined();
});
});

describe('getTags', () => {
describe('test `getBlogsTags`', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should test `getTags` function', async () => {
const data: TagsSearchResult = {
it('should test `getBlogsTags` function', async () => {
const data: BlogsTagsSearchResult = {
filteredTags: [
{
displayName: 'Tag 1',
Expand Down Expand Up @@ -55,15 +51,132 @@ describe('tags-api', () => {
});

const host = 'https://searchproxy.api.community.sap.com';
const result = await getTags(host);
const result = await getBlogsTags(host);

expect(requestUrl).toBe('https://searchproxy.api.community.sap.com/api/v1/tags');

await expect(result).toEqual({
expect(result).toEqual({
data: data,
error: undefined,
status: 'fetched'
});
});

it('should test `getBlogsTags` function with error', async () => {
const error = {
message: 'error message'
};

mockedAxios.get.mockImplementation(() => {
return Promise.reject(error);
});

const host = 'https://searchproxy.api.community.sap.com';
const result = await getBlogsTags(host);

expect(result).toEqual({
data: undefined,
error: 'error message',
status: 'error'
});
});

it('should test `getBlogsTags` function with empty host', async () => {
const result = await getBlogsTags('');

expect(result).toEqual({
data: undefined,
error: 'error message',
status: 'error'
});
});
});

describe('test `getTutorialsTags`', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should test `getTutorialsTags` function', async () => {
const data: TutorialsSearchResult = {
countGroups: 1,
countMissions: 1,
countTutorials: 1,
facets: {
topic: ['test']
},
group: '/group',
iconPath: {},
mission: '/mission',
numFound: 3,
result: [],
tags: {
'1': {
title: 'tag 1',
tagTitle: 'tag 1',
tagAlternativeTitles: ['tag1']
},
'2': {
title: 'tag 2',
tagTitle: 'tag 2',
tagAlternativeTitles: ['tag2']
},
'3': {
title: 'tag 3',
tagTitle: 'tag 3',
tagAlternativeTitles: ['tag3']
}
},
tutorialsNewFrom: new Date()
};

let requestUrl = '';
mockedAxios.get.mockImplementation((url) => {
requestUrl = url;
return Promise.resolve({ data });
});

const host = 'https://developers.sap.com';
const result = await getTutorialsTags(host);

expect(requestUrl).toBe(
'https://developers.sap.com/bin/sapdx/v3/solr/search?json={"rows":10,"start":0,"searchField":"","pagePath":"/content/developers/website/languages/en/tutorial-navigator","language":"en_us","addDefaultLanguage":true,"filters":[]}'
);

expect(result).toEqual({
data: data,
error: undefined,
status: 'fetched'
});
});

it('should test `getTutorialsTags` function with error', async () => {
const error = {
message: 'error message'
};

mockedAxios.get.mockImplementation(() => {
return Promise.reject(error);
});

const host = 'https://developers.sap.com';
const result = await getTutorialsTags(host);

expect(result).toEqual({
data: undefined,
error: 'error message',
status: 'error'
});
});

it('should test `getTutorialsTags` function with empty host', async () => {
const result = await getTutorialsTags('');

expect(result).toEqual({
data: undefined,
error: 'error message',
status: 'error'
});
});
});
});
2 changes: 0 additions & 2 deletions packages/ide-extension/src/knowledge-hub/actionsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
TAGS_FETCH_TUTORIALS_TAGS,
initialize,
fetchBlogs,
fetchHomeBlogs,
fetchTutorials,
fetchTutorialsTags,
fetchBlogsTags,
Expand Down Expand Up @@ -121,7 +120,6 @@ export class ActionHandler {
await this.panel.webview.postMessage(fetchBlogs.fulfilled(response.data));
if (action.query.searchTerm !== '') {
await this.panel.webview.postMessage(fetchBlogsTotalCount.fulfilled(response.data.totalCount));
await this.panel.webview.postMessage(fetchHomeBlogs.fulfilled(response.data));
} else {
await this.panel.webview.postMessage(fetchBlogsTotalCount.fulfilled(-1));
}
Expand Down
1 change: 0 additions & 1 deletion packages/types/src/const/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ export const fetchBlogs = createCoreAction<BlogsSearchResult>('blogs/fetch');
export const fetchBlogsTags = createCoreAction<BlogsTagsSearchResult>('tags/fetch/blogs-tags');
export const fetchTutorialsTags = createCoreAction<TutorialsSearchResult>('tags/fetch/tutorials-tags');
export const initBlogsQuery = createCoreAction<BlogFiltersEntry[]>('blogs/init/query');
export const initBlogsFilters = createCoreAction<BlogFiltersEntry[]>('blogs/init/filters');
export const initTutorialsFilters = createCoreAction<TutorialsTagWithTitle[]>('tutorials/init/filters');
export const fetchBlogsTotalCount = createCoreAction<number>('blogs/fetch-total-count');
export const fetchTutorialsTotalCount = createCoreAction<number>('tutorials/fetch-total-count');
1 change: 0 additions & 1 deletion packages/types/src/types/tags.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { get } from 'http';
import type { Error, FetchResponse } from './common.types';
import type { TutorialsTags, TutorialsSearchResult } from './tutorials.types';

Expand Down
1 change: 1 addition & 0 deletions packages/webapp/src/webview/features/tags/Tags.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,5 @@ export const getTagsTutorials = (state: RootState) => state.tags.tutorials;
export const getTagsTutorialsData = (state: RootState) => state.tags.tutorials.tags;
export const getTagsTutorialsPending = (state: RootState) => state.tags.tutorials.pending;
export const getTagsTutorialsError = (state: RootState) => state.tags.tutorials.error;

export default combineReducers({ blogs: blogsTags.reducer, tutorials: tutorialsTags.reducer });
31 changes: 30 additions & 1 deletion packages/webapp/test/__mocks__/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
export const appInitialState = {
appId: ''
app: {
appFilters: {},
appId: ''
},
ui: {
ready: false,
tabs: {
blogs: {
ariaLabel: 'BLOGS_TAB',
count: -1,
headerText: 'SAP Community Blogs',
key: 'blogs',
path: '/blogs'
},
home: {
ariaLabel: 'HOME_TAB',
count: -1,
headerText: 'Home',
key: 'home',
path: '/'
},
tutorials: {
ariaLabel: 'TUTORIALS_TAB',
count: -1,
headerText: 'SAP Tutorials',
key: 'tutorials',
path: '/tutorials'
}
}
}
};
30 changes: 10 additions & 20 deletions packages/webapp/test/__mocks__/blogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ export const blogsInitialState = {
pending: false
},
query: queryEmpty,
ui: uiNoFiltersNoLoading,
tags: []
ui: uiNoFiltersNoLoading
};

export const initialWithLoading = {
Expand All @@ -222,8 +221,7 @@ export const initialWithLoading = {
pending: false
},
query: queryEmpty,
ui: uiEmpty,
tags: []
ui: uiEmpty
};

export const initialWithPending = {
Expand All @@ -234,8 +232,7 @@ export const initialWithPending = {
pending: true
},
query: queryEmpty,
ui: uiNoFiltersNoLoading,
tags: []
ui: uiNoFiltersNoLoading
};

export const withNoDataNoError = {
Expand All @@ -246,8 +243,7 @@ export const withNoDataNoError = {
pending: false
},
query: queryEmpty,
ui: uiNoFiltersNoLoading,
tags: []
ui: uiNoFiltersNoLoading
};

export const withDataNoError = {
Expand All @@ -258,8 +254,7 @@ export const withDataNoError = {
pending: false
},
query: queryEmpty,
ui: uiNoFiltersNoLoading,
tags: []
ui: uiNoFiltersNoLoading
};

export const withDataNoErrorMenuOpened = {
Expand All @@ -270,8 +265,7 @@ export const withDataNoErrorMenuOpened = {
pending: false
},
query: queryEmpty,
ui: uiNoFiltersNoLoadingMenuOpened,
tags: []
ui: uiNoFiltersNoLoadingMenuOpened
};

export const withNoDataWithError = {
Expand All @@ -282,8 +276,7 @@ export const withNoDataWithError = {
pending: false
},
query: queryEmpty,
ui: uiNoFiltersNoLoading,
tags: []
ui: uiNoFiltersNoLoading
};

export const withDataWithFilter = {
Expand All @@ -294,8 +287,7 @@ export const withDataWithFilter = {
pending: false
},
query: queryWithFilter,
ui: uiWithFilters,
tags: tagsWithData
ui: uiWithFilters
};

export const withDataWithTags = {
Expand All @@ -306,8 +298,7 @@ export const withDataWithTags = {
pending: false
},
query: queryWithFilter,
ui: uiWithFilters,
tags: tagsWithData
ui: uiWithFilters
};

export const withDataNoErrorMultiplePage = {
Expand Down Expand Up @@ -338,8 +329,7 @@ export const withDataNoErrorMultiplePage = {
additionalManagedTags: [] as string[],
additionalUserTags: [] as string[]
},
ui: uiEmpty,
tags: []
ui: uiEmpty
};

export const blogsData = {
Expand Down
Loading

0 comments on commit b8b07d7

Please sign in to comment.