Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/app/courses/courses.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { CoursesComponent } from './courses.component';
import { DialogsDeleteComponent } from '../shared/dialogs/dialogs-delete.component';
import { RouterTestingModule } from '@angular/router/testing';
import { CouchService } from '../shared/couchdb.service';
import { HttpClientModule } from '@angular/common/http';

import { FormErrorMessagesComponent } from '../shared/form-error-messages.component';
import { FormErrorMessagesComponent } from '../shared/forms/form-error-messages.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from '../shared/material.module';
import { By } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import 'rxjs/add/observable/throw';
import { of } from 'rxjs';

describe('CoursesComponent', () => {
let component: CoursesComponent;
Expand Down Expand Up @@ -46,7 +43,7 @@ describe('CoursesComponent', () => {

// test getCourses()
it('should make a get request to couchService', () => {
getSpy = spyOn(couchService, 'get').and.returnValue(of(coursedata1).map).and.callThrough();
getSpy = spyOn(couchService, 'get').and.returnValue(of(coursedata1)).and.callThrough();
component.getCourses();
fixture.whenStable().then(() => {
fixture.detectChanges();
Expand Down Expand Up @@ -85,6 +82,24 @@ describe('CoursesComponent', () => {
expect(component.courses.data).toBe(component.courses.data.filter((coursedata1)));
});
});

it('filters courses by selected grade level', () => {
const gradeOneCourse: any = {
_id: 'grade-one',
doc: { gradeLevel: '1', courseTitle: 'Grade 1 Course' },
tags: []
};
const gradeTwelveCourse: any = {
_id: 'grade-twelve',
doc: { gradeLevel: '12', courseTitle: 'Grade 12 Course' },
tags: []
};

component.searchSelection.gradeLevel = [ '1' ];

expect(component.filterPredicate(gradeOneCourse, '')).toBe(true);
expect(component.filterPredicate(gradeTwelveCourse, '')).toBe(false);
});
/*
it('should show There was an error message deleting course', () => {
deleteSpy = spyOn(couchService, 'delete').and.returnValue(Rx.Observable.throw({ Error }));
Expand Down
34 changes: 25 additions & 9 deletions src/app/shared/table-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,31 @@ export const filterFieldExists = (filterFields: string[], trueIfExists: boolean)
};
};

const matchAllItems = (filterItems: string[], propItems: string[]) => {
return filterItems.reduce((isMatch, filter) => isMatch && propItems.indexOf(filter) > -1, true);
};

export const filterArrayField = (filterField: string, filterItems: string[]) => {
return (data: any, filter: string) => {
return matchAllItems(filterItems, getProperty(data, filterField) || []);
};
};
const normalizeToArray = (value: any): any[] => {
if (value === undefined || value === null) {
return [];
}

return Array.isArray(value) ? value : [ value ];
};

const matchAllItems = (filterItems: string[], propItems: any) => {
const normalizedFilterItems = normalizeToArray(filterItems);

if (normalizedFilterItems.length === 0) {
return true;
}

const normalizedPropItems = normalizeToArray(propItems);

return normalizedFilterItems.reduce((isMatch, filter) => isMatch && normalizedPropItems.indexOf(filter) > -1, true);
};

export const filterArrayField = (filterField: string, filterItems: string[]) => {
return (data: any, filter: string) => {
return matchAllItems(filterItems, getProperty(data, filterField));
};
};

export const filterTags = (filterControl: FormControl) => {
return (data: any, filter: string) => {
Expand Down
Loading