Adds tests for the pipes

parent da9ffa6d
......@@ -2,6 +2,7 @@ import { HttpClientTestingModule } from '@angular/common/http/testing';
import { PLATFORM_ID } from '@angular/core';
import { async, TestBed } from '@angular/core/testing';
import { JWT_OPTIONS, JwtModule } from '@auth0/angular-jwt';
import { Filter, Language } from '../../lib/enums/enums';
import { jwtOptionsFactory } from '../../lib/jwt.factory';
import { I18nManagerApiService } from '../../service/api/i18n-manager/i18n-manager-api.service';
import { LanguageLoaderService } from '../../service/language-loader/language-loader.service';
......@@ -13,6 +14,22 @@ import { FilterKeysPipe } from './filter-keys.pipe';
describe('FilterKeysPipe', () => {
let pipe: FilterKeysPipe;
const mockValues = [
{
key: 'test1key',
value: {
[Language.DE]: 'test1value',
[Language.EN]: 'test1value',
[Language.FR]: 'test1value',
},
}, {
key: 'test2key',
value: {
[Language.DE]: 'test2value',
[Language.EN]: 'test2value',
},
},
];
beforeEach(async(() => {
TestBed.configureTestingModule({
......@@ -26,7 +43,12 @@ describe('FilterKeysPipe', () => {
}), HttpClientTestingModule,
],
providers: [
LanguageLoaderService, I18nManagerApiService, ProjectLoaderService, {
{
provide: LanguageLoaderService,
useValue: {
language: Language.DE,
},
}, I18nManagerApiService, ProjectLoaderService, {
provide: UserService,
useValue: {},
}, {
......@@ -47,4 +69,32 @@ describe('FilterKeysPipe', () => {
it('create an instance', () => {
expect(pipe).toBeTruthy();
});
it('should return the original value if no filter is set', () => {
expect(pipe.transform(mockValues, Filter.None)).toEqual(mockValues);
});
it('should return true if the current language setting has more keys then the filtered one', () => {
expect(pipe.transform(mockValues, Filter.InvalidKeys).length).toEqual(0);
});
it('should return false if the language files have DE keys', () => {
expect(pipe.transform(mockValues, Filter.InvalidDE).length).toEqual(0);
});
it('should return false if the language files have EN keys', () => {
expect(pipe.transform(mockValues, Filter.InvalidEN).length).toEqual(0);
});
it('should return false if the language files have FR keys', () => {
expect(pipe.transform(mockValues, Filter.InvalidFr).length).toEqual(1);
});
it('should return false if the language files do not have ES keys', () => {
expect(pipe.transform(mockValues, Filter.InvalidES).length).toBeGreaterThan(0);
});
it('should return false if the language files do not have IT keys', () => {
expect(pipe.transform(mockValues, Filter.InvalidIt).length).toBeGreaterThan(0);
});
});
......@@ -9,7 +9,7 @@ export class FilterKeysPipe implements PipeTransform {
constructor(private languageLoaderService: LanguageLoaderService) {}
public transform(value: Array<any>, filterSetting?: Filter): any {
public transform<T>(value: Array<T>, filterSetting?: Filter): Array<T> {
switch (filterSetting) {
case Filter.None:
return value;
......
......@@ -2,8 +2,29 @@ import { TranslateServiceMock } from '../../../_mocks/TranslateServiceMock';
import { GenericFilterPipe } from './generic-filter.pipe';
describe('GenericFilterPipe', () => {
const stringMock = ['test1', 'test2'];
const objectMock = [{ key: 'test1' }, { key: 'test2' }];
it('create an instance', () => {
const pipe = new GenericFilterPipe(new TranslateServiceMock());
expect(pipe).toBeTruthy();
});
it('should return the original value if no args where provided', () => {
const pipe = new GenericFilterPipe(new TranslateServiceMock());
expect(pipe.transform(stringMock, {})).toEqual(stringMock);
});
it('should return a match by a string args', () => {
const pipe = new GenericFilterPipe(new TranslateServiceMock());
expect(pipe.transform(stringMock, 'test1').length).toEqual(1);
});
it('should return a match by an object as arg containing strings', () => {
const pipe = new GenericFilterPipe(new TranslateServiceMock());
expect(pipe.transform(objectMock, {
$translateKeys: true,
key: 'test1',
}).length).toEqual(1);
});
});
......@@ -9,7 +9,7 @@ export class GenericFilterPipe implements PipeTransform {
public constructor(private translateService: TranslateService) {}
public transform<T extends any>(value: Array<T>, args?: string | { [key: string]: any }): Array<T> {
if (!args || (typeof args === 'string' && !args.length) || !Object.keys(args).length) {
if (!args || !Object.keys(args).length) {
return value;
}
......
import { JustAFewPipe } from './justafew.pipe';
describe('JustafewPipe', () => {
const mock: Array<number> = Array(5).fill(5);
const maxLength = 2;
it('create an instance', () => {
const pipe = new JustAFewPipe();
expect(pipe).toBeTruthy();
});
it('should return the original value if no limiter was provided', () => {
const pipe = new JustAFewPipe();
expect(pipe.transform(mock, 0)).toEqual(mock);
});
it('should return a subset of the value with a length of maxLength', () => {
const pipe = new JustAFewPipe();
expect(pipe.transform(mock, maxLength).length).toEqual(maxLength);
});
});
......@@ -4,7 +4,7 @@ import { Pipe, PipeTransform } from '@angular/core';
name: 'justafew',
})
export class JustAFewPipe implements PipeTransform {
public transform(value: Array<any>, start: number): Array<any> {
public transform<T>(value: Array<T>, start: number): Array<T> {
if (!start || value.length < start) {
return value;
}
......
import { QuizState } from '../../lib/enums/QuizState';
import { QuizVisibility } from '../../lib/enums/QuizVisibility';
import { IAdminQuiz } from '../../lib/interfaces/quizzes/IAdminQuiz';
import { QuizAdminFilterPipe } from './quiz-admin-filter.pipe';
describe('QuizAdminFilterPipe', () => {
const adminQuizzes: Array<IAdminQuiz> = [
{
state: QuizState.Inactive,
answerAmount: 1,
expiry: null,
id: null,
memberAmount: 1,
name: 'test-quiz-inactive-1',
questionAmount: 1,
visibility: QuizVisibility.Account,
}, {
state: QuizState.Inactive,
answerAmount: 1,
expiry: null,
id: null,
memberAmount: 1,
name: 'demo quiz 1',
questionAmount: 1,
visibility: QuizVisibility.Account,
}, {
state: QuizState.Inactive,
answerAmount: 1,
expiry: null,
id: null,
memberAmount: 1,
name: 'abcd 1',
questionAmount: 1,
visibility: QuizVisibility.Account,
}, {
state: QuizState.Inactive,
answerAmount: 1,
expiry: null,
id: null,
memberAmount: 1,
name: 'find-me',
questionAmount: 1,
visibility: QuizVisibility.Account,
}, {
state: QuizState.Running,
answerAmount: 1,
expiry: null,
id: null,
memberAmount: 1,
name: 'test-quiz-running-1',
questionAmount: 1,
visibility: QuizVisibility.Account,
}, {
state: QuizState.Active,
answerAmount: 1,
expiry: null,
id: null,
memberAmount: 1,
name: 'test-quiz-active-1',
questionAmount: 1,
visibility: QuizVisibility.Account,
},
];
it('create an instance', () => {
const pipe = new QuizAdminFilterPipe();
expect(pipe).toBeTruthy();
});
it('should find the regular quizzes plus the demo quizzes in the values', () => {
const pipe = new QuizAdminFilterPipe();
const result = pipe.transform(adminQuizzes, { filterDemoQuiz: true });
expect(result.length).toEqual(5);
expect(result).toContain(adminQuizzes[1]);
});
it('should find the regular quizzes plus the abcd quizzes in the values', () => {
const pipe = new QuizAdminFilterPipe();
const result = pipe.transform(adminQuizzes, { filterAbcdQuiz: true });
expect(result.length).toEqual(5);
expect(result).toContain(adminQuizzes[2]);
});
it('should return the original values if no args are provided', () => {
const pipe = new QuizAdminFilterPipe();
expect(pipe.transform(adminQuizzes, {})).toEqual(adminQuizzes);
});
it('should find a quiz by case insensitive name', () => {
const pipe = new QuizAdminFilterPipe();
expect(pipe.transform(adminQuizzes, { filterQuizName: 'FIND-me' }).length).toEqual(1);
});
it('should find a running or active quiz in the values', () => {
const pipe = new QuizAdminFilterPipe();
expect(pipe.transform(adminQuizzes, { filterActiveQuiz: true }).length).toEqual(2);
});
});
......@@ -3,14 +3,21 @@ import { checkABCDOrdering } from '../../lib/checkABCDOrdering';
import { QuizState } from '../../lib/enums/QuizState';
import { IAdminQuiz } from '../../lib/interfaces/quizzes/IAdminQuiz';
interface IFilterArgs {
filterDemoQuiz?: boolean;
filterAbcdQuiz?: boolean;
filterActiveQuiz?: boolean;
filterQuizName?: string;
}
@Pipe({
name: 'quizAdminFilter',
pure: false,
})
export class QuizAdminFilterPipe implements PipeTransform {
public transform(value: Array<IAdminQuiz>, args?: any): Array<IAdminQuiz> {
if (!value || !value.length || !args || !Object.keys(args).length) {
public transform(value: Array<IAdminQuiz>, args: IFilterArgs): Array<IAdminQuiz> {
if (!value || !Object.keys(args || {}).length) {
return value;
}
......
import { SearchFilterPipe } from './search-filter.pipe';
describe('SearchFilterPipe', () => {
const objectMock = [{ key: 'find-me' }, { key: 'dont find me' }];
const stringMock = [
'this is a long test but it will be found since it includes find-me',
'this is a long test but it will wont be found since it does not include find me',
];
it('create an instance', () => {
const pipe = new SearchFilterPipe();
expect(pipe).toBeTruthy();
});
it('should find a key in an object', () => {
const pipe = new SearchFilterPipe();
expect(pipe.transform(objectMock, 'find-me').length).toEqual(1);
});
it('should not find a non-matching key in an object', () => {
const pipe = new SearchFilterPipe();
expect(pipe.transform(objectMock, 'dont-find-me').length).toEqual(0);
});
it('should find a key in a string', () => {
const pipe = new SearchFilterPipe();
expect(pipe.transform(stringMock, 'find-me').length).toEqual(1);
});
it('should not find a non-matching string', () => {
const pipe = new SearchFilterPipe();
expect(pipe.transform(stringMock, 'dont-find-me').length).toEqual(0);
});
});
import { Pipe, PipeTransform } from '@angular/core';
interface ISearchType {
key: string;
}
@Pipe({
name: 'searchFilter',
pure: false,
})
export class SearchFilterPipe implements PipeTransform {
public transform(value: Array<any>, args?: string): Array<any> {
if (!args || !args.length) {
public transform<T extends ISearchType | string>(value: Array<T>, searchFilter?: string): Array<T> {
if (!searchFilter) {
return value;
}
return value.filter(val => {
if (val.key) {
return val.key.indexOf(args) > -1;
if (typeof val === 'string') {
return val.includes(searchFilter);
}
return val.indexOf(args) > -1;
return (
val as ISearchType
).key.includes(searchFilter);
});
}
......
import { SortPipe } from './sort.pipe';
describe('SortPipe', () => {
const objectMock = [{ key: 'sort-2' }, { key: 'sort-1' }];
it('create an instance', () => {
const pipe = new SortPipe();
expect(pipe).toBeTruthy();
});
it('should sort an object by the "key" property ascending', () => {
const pipe = new SortPipe();
expect(pipe.transform(objectMock)[0].key).toEqual('sort-1');
});
});
......@@ -5,7 +5,7 @@ import { Pipe, PipeTransform } from '@angular/core';
})
export class SortPipe implements PipeTransform {
public transform(value: Array<any>, args?: string): Array<any> {
public transform<T extends { key: string }>(value: Array<T>): Array<T> {
return value.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
}
......
......@@ -13,6 +13,20 @@ import { UnusedKeyFilterPipe } from './unused-key-filter.pipe';
describe('UnusedKeyFilterPipe', () => {
let pipe: UnusedKeyFilterPipe;
const mockValues = [
{
key: 'test1key',
value: 'test1value',
},
{
key: 'test2key',
value: 'test2value',
},
{
key: 'unused-key',
value: 'unused-value',
},
];
beforeEach(async(() => {
TestBed.configureTestingModule({
......@@ -26,7 +40,12 @@ describe('UnusedKeyFilterPipe', () => {
}), HttpClientTestingModule,
],
providers: [
LanguageLoaderService, I18nManagerApiService, ProjectLoaderService, {
{
provide: LanguageLoaderService,
useValue: {
unusedKeys: ['unused-key'],
},
}, I18nManagerApiService, ProjectLoaderService, {
provide: UserService,
useValue: {},
}, {
......@@ -47,4 +66,12 @@ describe('UnusedKeyFilterPipe', () => {
it('create an instance', () => {
expect(pipe).toBeTruthy();
});
it('should return only the unused keys of the value', () => {
expect(pipe.transform(mockValues, true).length).toEqual(1);
});
it('should return only the used keys of the value', () => {
expect(pipe.transform(mockValues).length).toEqual(2);
});
});
......@@ -7,8 +7,8 @@ import { LanguageLoaderService } from '../../service/language-loader/language-lo
export class UnusedKeyFilterPipe implements PipeTransform {
constructor(private languageLoaderService: LanguageLoaderService) {}
public transform(value: Array<any>, args?: any): any {
if (args) {
public transform<T>(value: Array<T>, filterUnused?: boolean): Array<T> {
if (filterUnused) {
return value.filter(elem => this.isUnused(elem));
}
......@@ -16,6 +16,6 @@ export class UnusedKeyFilterPipe implements PipeTransform {
}
private isUnused(elem): boolean {
return !!this.languageLoaderService.unusedKeys.find(unusedKey => unusedKey === elem.key);
return this.languageLoaderService.unusedKeys.some(unusedKey => unusedKey === elem.key);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment