Skip to content

Commit

Permalink
BW-757 #comment changes for leaderboard unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
piya99 committed May 7, 2020
1 parent 825d21b commit 7bbe161
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 20 deletions.
34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/@angular/cli/bin/ng",
"cwd": "${workspaceFolder}",
"args": [
"test",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/@angular/cli/bin/ng",
"cwd": "${workspaceFolder}",
"args": [
"test",
"--testPathPattern=${fileBasenameNoExtension}",
"--runInBand",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
}
]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"ng": "ng",
"api": "json-server --watch src/db.json",
"lint": "tslint \"src/**/*.ts\" --project src/tsconfig.json --type-check && tslint \"e2e/**/*.ts\" --project e2e/tsconfig.json --type-check",
"test": "ng test",
"test:watch": "jest --watch",
"test": "ng test --coverage",
"test:watch": "ng test --watch",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,15 @@ describe('LeaderboardComponent', () => {

it('score board should be set when values are emitted', () => {
const user = TEST_DATA.userList[0];
const categories = [];
for (const key in TEST_DATA.categoryDictionary) {
if (TEST_DATA.categoryDictionary.hasOwnProperty(key)) {
categories.push(TEST_DATA.categoryDictionary[key]);
}
}
mockStore.overrideSelector<AppState, Partial<CoreState>>(appState.coreState, {
topTopics: [],
categories: [],
categories: categories,
user: user
});

Expand All @@ -220,35 +226,83 @@ describe('LeaderboardComponent', () => {
topTopics.push({id: data.id, categoryName: data.key, type: data.key});
});

mockStore.overrideSelector<AppState, Partial<CoreState>>(appState.coreState, {
user: user,
topTopics: topTopics,
categories: categories
});

mockStore.overrideSelector<AppState, Partial<DashboardState>>(appState.dashboardState, {
scoreBoard: TEST_DATA.leaderBoard
});

mockStore.refreshState();

const leaderBoardCat = [];
const items = [];
TEST_DATA.leaderBoard.map(
(leaderBoard: any) => {
if (leaderBoard.users.length > 0) {
leaderBoardCat.push(leaderBoard['type'] === 'category' ? TEST_DATA.categoryDictionary[leaderBoard.id].categoryName :
(`${leaderBoard.id.charAt(0).toUpperCase()}${leaderBoard.id.slice(1)}`));
}
items.push(leaderBoard['type'] === 'category' ? TEST_DATA.categoryDictionary[leaderBoard.id].categoryName :
(`${leaderBoard.id.charAt(0).toUpperCase()}${leaderBoard.id.slice(1)}`));
}
);


expect(component.leaderBoardCat).toEqual(leaderBoardCat);
});

it('leaderboard category should be set when leaderboard contains more than one user values are emitted', () => {
const user = TEST_DATA.userList[0];
const categories = [];
for (const key in TEST_DATA.categoryDictionary) {
if (TEST_DATA.categoryDictionary.hasOwnProperty(key)) {
categories.push(TEST_DATA.categoryDictionary[key]);
}
}

const topTopics = [];
TEST_DATA.topTopics.forEach(data => {
topTopics.push({id: data.id, categoryName: data.key, type: data.key});
});

mockStore.overrideSelector<AppState, Partial<CoreState>>(appState.coreState, {
user: user,
topTopics: topTopics,
categories: categories
});

mockStore.overrideSelector<AppState, Partial<DashboardState>>(appState.dashboardState, {
scoreBoard: TEST_DATA.leaderBoard
});

mockStore.refreshState();

const leaderBoardCat = [];
const items = [];
const leaderBoardStatDictArr = {};
TEST_DATA.leaderBoard.map(
(leaderBoard: any) => {
if (leaderBoard.users.length > 0) {
leaderBoardCat.push(leaderBoard['type'] === 'category' ? TEST_DATA.categoryDictionary[leaderBoard.id].categoryName :
(`${leaderBoard.id.charAt(0).toUpperCase()}${leaderBoard.id.slice(1)}`));
}
items.push(leaderBoard['type'] === 'category' ? TEST_DATA.categoryDictionary[leaderBoard.id].categoryName :
(`${leaderBoard.id.charAt(0).toUpperCase()}${leaderBoard.id.slice(1)}`));
}
);
TEST_DATA.leaderBoard.filter((leaderBoardStatDict: any) => {
leaderBoardStatDictArr[leaderBoardStatDict['type'] === 'category' ?
TEST_DATA.categoryDictionary[leaderBoardStatDict.id].categoryName :
`${leaderBoardStatDict.id.charAt(0).toUpperCase()}${leaderBoardStatDict.id.slice(1)}`] =
leaderBoardStatDict.users;
});

// const leaderBoardCat = [];
// const items = [];
// TEST_DATA.leaderBoard.map(
// (leaderBoard: any) => {
// if (leaderBoard.users.length > 0) {
// leaderBoardCat.push(leaderBoard['type'] === 'category' ? TEST_DATA.categoryDictionary[leaderBoard.id].categoryName :
// (`${leaderBoard.id.charAt(0).toUpperCase()}${leaderBoard.id.slice(1)}`));
// }
// items.push(leaderBoard['type'] === 'category' ? TEST_DATA.categoryDictionary[leaderBoard.id].categoryName :
// (`${leaderBoard.id.charAt(0).toUpperCase()}${leaderBoard.id.slice(1)}`));
// }
// );


mockStore.refreshState();
expect(component.leaderBoardStatDictArray).toEqual(TEST_DATA.leaderBoard);
expect(component.leaderBoardCat).toEqual(leaderBoardCat);
expect(component.leaderBoardStatDict).toEqual(leaderBoardStatDictArr);
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class Leaderboard implements OnDestroy {
})
)
.subscribe(lbsStat => {
if (lbsStat) {
if (lbsStat && Object.entries(this.categoryDict).length > 0) {
this.leaderBoardStatDictArray = lbsStat;
this.items = [];
this.leaderBoardStatDictArray.map(
Expand Down
1 change: 1 addition & 0 deletions projects/trivia/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowJs": true,
"lib": [
"es2018",
Expand Down

0 comments on commit 7bbe161

Please sign in to comment.