Skip to content

Commit 1d453f6

Browse files
author
Nilesh Patel
committed
jest integration
1 parent af2833f commit 1d453f6

8 files changed

+80
-8
lines changed

jest.config.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @TODO: try to add ts-check
2+
const jestConfig = {
3+
preset: 'jest-preset-angular',
4+
setupTestFrameworkScriptFile: '<rootDir>/src/setup-jest.ts',
5+
testMatch: [
6+
'<rootDir>/src/**/__tests__/**/*.+(ts|js)?(x)',
7+
'<rootDir>/src/**/+(*.)+(spec|test).+(ts|js)?(x)',
8+
],
9+
// // moduleNameMapper: {
10+
// // 'app/(.*)': '<rootDir>/src/app/$1',
11+
// // 'assets/(.*)': '<rootDir>/src/assets/$1',
12+
// // 'environments/(.*)': '<rootDir>/src/environments/$1',
13+
// // },
14+
// // transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
15+
coveragePathIgnorePatterns: [
16+
'<rootDir>/node_modules/',
17+
'<rootDir>/out-tsc/',
18+
'<rootDir>/src/.*(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$',
19+
'src/(setup-jest|jest-global-mocks).ts',
20+
],
21+
};
22+
23+
module.exports = jestConfig;

package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
"ng": "ng",
1010
"start": "ng serve",
1111
"build": "ng build",
12-
"test": "ng test",
12+
"ng:test": "ng test",
1313
"lint": "ng lint",
14+
"test": "jest --watch",
15+
"test:ci": "jest --runInBand",
16+
"test:coverage": "jest --coverage",
1417
"build:prod": "ng build --prod --base-href https://nileshpatel17.github.io/ng-multiselect-dropdown/",
1518
"clear:lib": "rimraf dist-lib",
1619
"copyfiles": "copyfiles -u 1 ./dist-lib/**/*.* node_modules/ng-multiselect-dropdown",
@@ -84,6 +87,8 @@
8487
"ts-node": "~3.0.4",
8588
"tslint": "~5.3.2",
8689
"typescript": "2.6.2",
87-
"zone.js": "^0.8.25"
90+
"zone.js": "^0.8.25",
91+
"jest": "^23.4.1",
92+
"jest-preset-angular": "^5.2.3"
8893
}
89-
}
94+
}

src/app/app.component.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { DropdownSettings } from '../ng-multiselect-dropdown/src/multiselect.component';
32

43
@Component({
54
selector: 'app-root',

src/app/components/select/multiple-demo.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FormBuilder, FormGroup } from '@angular/forms';
22
import { Component, OnInit } from '@angular/core';
3+
import { IDropdownSettings } from '../../../ng-multiselect-dropdown/src';
34

45
@Component({
56
selector: 'multiple-demo',
@@ -13,7 +14,7 @@ export class MultipleDemoComponent implements OnInit {
1314
limitSelection = false;
1415
cities: Array<any> = [];
1516
selectedItems: Array<any> = [];
16-
dropdownSettings: any = {};
17+
dropdownSettings: IDropdownSettings = {};
1718
htmlCode = `
1819
&lt;form [formGroup]="myForm"&gt;
1920
&lt;ng-multiselect-dropdown
@@ -90,7 +91,7 @@ export class MultipleDemoComponent implements OnInit {
9091
}
9192
`;
9293

93-
constructor(private fb: FormBuilder) {}
94+
constructor(private fb: FormBuilder) { }
9495

9596
ngOnInit() {
9697
this.cities = [

src/jest-global-mocks.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @ts-ignore
2+
global.CSS = null;
3+
4+
const webStorageMock = () => {
5+
let storage: Record<string, any> = {};
6+
return {
7+
getItem: (key: string) => (key in storage ? storage[key] : null),
8+
setItem: (key: string, value: any) => (storage[key] = value || ''),
9+
removeItem: (key: string) => delete storage[key],
10+
clear: () => (storage = {}),
11+
};
12+
};
13+
14+
Object.defineProperty(window, 'localStorage', { value: webStorageMock() });
15+
Object.defineProperty(window, 'sessionStorage', { value: webStorageMock() });
16+
Object.defineProperty(document, 'doctype', {
17+
value: '<!DOCTYPE html>',
18+
});
19+
Object.defineProperty(window, 'getComputedStyle', {
20+
value: () => {
21+
return {
22+
display: 'none',
23+
appearance: ['-webkit-appearance'],
24+
};
25+
},
26+
});
27+
/**
28+
* ISSUE: https://github.com/angular/material2/issues/7101
29+
* Workaround for JSDOM missing transform property
30+
*/
31+
Object.defineProperty(document.body.style, 'transform', {
32+
value: () => {
33+
return {
34+
enumerable: true,
35+
configurable: true,
36+
};
37+
},
38+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test('sanity test', () => {
2+
expect(1).toBe(1)
3+
})

src/ng-multiselect-dropdown/test/multi-select.component.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Ng2MultiSelectDropdownMultipleSelect {
7474
singleSelection: false,
7575
idField: 'item_id',
7676
textField: 'item_text',
77-
selectAllText: 'Select-All',
77+
selectAllText: 'Select All',
7878
unSelectAllText: 'UnSelect All',
7979
badgeShowLimit: 3,
8080
disabled: false,
@@ -259,7 +259,7 @@ describe('ng-multiselect-component', function() {
259259
it('should have custom placeholder for "select all text" button', () => {
260260
fixture.whenStable().then(() => {
261261
const sel = document.querySelector('.select-all-text') as HTMLElement;
262-
expect(sel.innerText).toContain('Select-All');
262+
expect(sel.innerText).toContain('Select All');
263263
});
264264
});
265265
});

src/setup-jest.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'jest-preset-angular';
2+
3+
import './jest-global-mocks'; // browser mocks globally available for every test

0 commit comments

Comments
 (0)