Skip to content

Commit aaf92ff

Browse files
committed
add unit tests
1 parent 300c842 commit aaf92ff

File tree

5 files changed

+194
-4
lines changed

5 files changed

+194
-4
lines changed

package.json

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,45 @@
1919
"build": "npm run build:js && npm run build:source && npm run build:aot && rimraf tmp ",
2020
"build:source": "node config/source.js",
2121
"build:js": "rollup -c",
22-
"build:aot": "ngc -p config/aot.json"
22+
"build:aot": "ngc -p config/aot.json",
23+
"test": "jest"
2324
},
2425
"dependencies": {
2526
"@activewidgets/frameworks": "https://cdn.activewidgets.com/packages/frameworks-0.0.6.tgz",
2627
"@activewidgets/datagrid": "https://cdn.activewidgets.com/packages/datagrid-0.0.6.tgz",
2728
"@angular/core": "*"
2829
},
2930
"devDependencies": {
30-
"@angular/compiler": "*",
31-
"@angular/compiler-cli": "*",
31+
"@activewidgets/testing": "^0.0.1",
32+
"@angular/common": "^8.0.0",
33+
"@angular/compiler": "^8.0.0",
34+
"@angular/compiler-cli": "^8.0.0",
35+
"@angular/platform-browser": "^8.0.0",
36+
"@angular/platform-browser-dynamic": "^8.0.0",
3237
"@babel/core": "^7.0.0",
3338
"@babel/preset-env": "^7.0.0",
3439
"rimraf": "^2.0.0",
3540
"rollup": "^1.0.0",
3641
"rollup-plugin-babel": "^4.0.0",
3742
"rollup-plugin-node-resolve": "^4.0.0",
3843
"rxjs": "^6.0.0",
39-
"typescript": "~3.2.0"
44+
"typescript": ">=3.4 <3.6",
45+
"zone.js": "~0.9.1"
46+
},
47+
"jest": {
48+
"projects": [
49+
{
50+
"displayName": "Unit",
51+
"moduleNameMapper": {
52+
"@activewidgets/components": "<rootDir>/",
53+
"@activewidgets/testing": "<rootDir>/test/adapter/angular.js"
54+
},
55+
"preset": "@activewidgets/testing",
56+
"setupFilesAfterEnv": [
57+
"<rootDir>/test/adapter/setup.js"
58+
]
59+
}
60+
]
4061
},
4162
"repository": {
4263
"type": "git",

test/adapter/angular.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
3+
import {getQueriesForElement} from '@testing-library/dom';
4+
import {TestBed} from '@angular/core/testing';
5+
import {Module} from '@activewidgets/components';
6+
7+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
8+
9+
TestBed.initTestEnvironment(
10+
BrowserDynamicTestingModule,
11+
platformBrowserDynamicTesting()
12+
);
13+
14+
15+
export function render(Component, props){
16+
17+
TestBed.configureTestingModule({
18+
imports: [Module]
19+
});
20+
21+
let fixture = TestBed.createComponent(Component),
22+
component = fixture.componentInstance;
23+
24+
for(let i in props){
25+
component[i] = props[i];
26+
}
27+
28+
fixture.detectChanges();
29+
30+
return {
31+
...getQueriesForElement(document.body)
32+
}
33+
}
34+

test/adapter/metadata-patch.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
const METADATA_KEY_PARAMTYPES = "design:paramtypes";
3+
const CTOR_PARAMETERS_JPA = "ctorParametersJPA";
4+
const reflect = global.Reflect;
5+
6+
if (!reflect.metadata && !reflect.getOwnMetadata) {
7+
reflect.metadata = (metadataKey, metadataValue) => (target, key) => {
8+
if (metadataKey === METADATA_KEY_PARAMTYPES && key === undefined) { // key undefined is ctor
9+
target[CTOR_PARAMETERS_JPA] = metadataValue;
10+
}
11+
}
12+
reflect.getOwnMetadata = (metadata, target, key) => {
13+
if (metadata === METADATA_KEY_PARAMTYPES && key === undefined) { // key undefined is ctor
14+
return target[CTOR_PARAMETERS_JPA];
15+
}
16+
}
17+
}

test/adapter/setup.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
require('@testing-library/jest-dom/extend-expect');
3+
require('./metadata-patch.js');
4+
require('zone.js/dist/zone.js');
5+
require('zone.js/dist/proxy.js');
6+
require('zone.js/dist/sync-test');
7+
require('zone.js/dist/async-test');
8+
require('zone.js/dist/fake-async-test');
9+
require('./zone-patch.js');
10+

test/adapter/zone-patch.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Patch Jest's describe/test/beforeEach/afterEach functions so test code
3+
* always runs in a testZone (ProxyZone).
4+
*/
5+
6+
if (Zone === undefined) {
7+
throw new Error('Missing: Zone (zone.js)');
8+
}
9+
if (jest === undefined) {
10+
throw new Error(
11+
'Missing: jest.\n' +
12+
'This patch must be included in a script called with ' +
13+
'`setupTestFrameworkScriptFile` in Jest config.'
14+
);
15+
}
16+
if (jest['__zone_patch__'] === true) {
17+
throw new Error("'jest' has already been patched with 'Zone'.");
18+
}
19+
20+
jest['__zone_patch__'] = true;
21+
const SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
22+
const ProxyZoneSpec = Zone['ProxyZoneSpec'];
23+
24+
if (SyncTestZoneSpec === undefined) {
25+
throw new Error('Missing: SyncTestZoneSpec (zone.js/dist/sync-test)');
26+
}
27+
if (ProxyZoneSpec === undefined) {
28+
throw new Error('Missing: ProxyZoneSpec (zone.js/dist/proxy.js)');
29+
}
30+
31+
const env = global;
32+
const ambientZone = Zone.current;
33+
34+
// Create a synchronous-only zone in which to run `describe` blocks in order to
35+
// raise an error if any asynchronous operations are attempted
36+
// inside of a `describe` but outside of a `beforeEach` or `it`.
37+
const syncZone = ambientZone.fork(new SyncTestZoneSpec('jest.describe'));
38+
function wrapDescribeInZone(describeBody) {
39+
return function() {
40+
return syncZone.run(describeBody, null, arguments);
41+
};
42+
}
43+
44+
// Create a proxy zone in which to run `test` blocks so that the tests function
45+
// can retroactively install different zones.
46+
const testProxyZone = ambientZone.fork(new ProxyZoneSpec());
47+
function wrapTestInZone(testBody) {
48+
if (testBody === undefined) {
49+
return;
50+
}
51+
return testBody.length === 0
52+
? () => testProxyZone.run(testBody, null)
53+
: done => testProxyZone.run(testBody, null, [done]);
54+
}
55+
56+
const bindDescribe = originalJestFn =>
57+
function() {
58+
const eachArguments = arguments;
59+
return function(description, specDefinitions, timeout) {
60+
arguments[1] = wrapDescribeInZone(specDefinitions);
61+
return originalJestFn.apply(this, eachArguments).apply(this, arguments);
62+
};
63+
};
64+
65+
['xdescribe', 'fdescribe', 'describe'].forEach(methodName => {
66+
const originaljestFn = env[methodName];
67+
env[methodName] = function(description, specDefinitions, timeout) {
68+
arguments[1] = wrapDescribeInZone(specDefinitions);
69+
return originaljestFn.apply(this, arguments);
70+
};
71+
env[methodName].each = bindDescribe(originaljestFn.each);
72+
if (methodName === 'describe') {
73+
env[methodName].only = env['fdescribe'];
74+
env[methodName].skip = env['xdescribe'];
75+
env[methodName].only.each = bindDescribe(originaljestFn.only.each);
76+
env[methodName].skip.each = bindDescribe(originaljestFn.skip.each);
77+
}
78+
});
79+
80+
['xit', 'fit', 'xtest', 'test', 'it'].forEach(methodName => {
81+
const originaljestFn = env[methodName];
82+
env[methodName] = function(description, specDefinitions, timeout) {
83+
arguments[1] = wrapTestInZone(specDefinitions);
84+
return originaljestFn.apply(this, arguments);
85+
};
86+
// The revised method will be populated to the final each method, so we only declare the method that in the new globals
87+
env[methodName].each = originaljestFn.each;
88+
89+
if (methodName === 'test' || methodName === 'it') {
90+
env[methodName].only = env['fit'];
91+
env[methodName].only.each = originaljestFn.only.each;
92+
93+
env[methodName].skip = env['xit'];
94+
env[methodName].skip.each = originaljestFn.skip.each;
95+
96+
env[methodName].todo = function(description) {
97+
return originaljestFn.todo.apply(this, arguments);
98+
};
99+
}
100+
});
101+
102+
['beforeEach', 'afterEach', 'beforeAll', 'afterAll'].forEach(methodName => {
103+
const originaljestFn = env[methodName];
104+
env[methodName] = function(specDefinitions, timeout) {
105+
arguments[0] = wrapTestInZone(specDefinitions);
106+
return originaljestFn.apply(this, arguments);
107+
};
108+
});

0 commit comments

Comments
 (0)