Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: integrate latest rxa specs & upgrade test setup #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
33,173 changes: 25,066 additions & 8,107 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"tslib": "^2.3.1"
},
"devDependencies": {
"@angular/core": "^15",
"@angular/compiler-cli": "^15",
"@angular/platform-browser-dynamic": "^15",
"@angular-devkit/build-angular": "^15",
"jest-preset-angular": "^12",
"jest-environment-jsdom": "29.4.3",
"@jscutlery/semver": "^2.25.2",
"@nrwl/cli": "14.1.9",
"@nrwl/eslint-plugin-nx": "^14.1.9",
Expand All @@ -21,24 +27,24 @@
"@nrwl/linter": "14.1.9",
"@nrwl/nx-cloud": "latest",
"@nrwl/workspace": "14.1.9",
"@types/jest": "28.1.0",
"@types/jest": "28.1.8",
"@types/lodash": "^4.14.182",
"@types/node": "16.11.7",
"@typescript-eslint/eslint-plugin": "5.10.2",
"@typescript-eslint/parser": "5.10.2",
"@typescript-eslint/eslint-plugin": "~5.18.0",
"@typescript-eslint/parser": "~5.18.0",
"cpy-cli": "^4.1.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.5.0",
"gzip": "^0.1.0",
"jest": "^27.5.1",
"jest": "^28.1.3",
"lodash": "^4.17.21",
"nx": "14.1.9",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"rollup": "^2.75.5",
"ts-jest": "^27.1.5",
"ts-jest": "^28.0.8",
"ts-node": "^10.8.0",
"typescript": "~4.6.2",
"typescript": "~4.9.5",
"uglify-js": "^3.15.5"
}
}
4 changes: 2 additions & 2 deletions packages/rxjs-zone-less/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
setupFilesAfterEnv: ['<rootDir>/spec/test-setup.ts'],
transform: {
'^.+\\.[tj]s$': 'ts-jest',
'^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'js', 'html'],
transformIgnorePatterns: ['node_modules/(?!.*.mjs$)'],
coverageDirectory: '../../coverage/packages/rxjs-zone-less',
};
25 changes: 20 additions & 5 deletions packages/rxjs-zone-less/spec/scheduler/asapScheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ import { RxTestScheduler } from '../../src/lib/testing/test-scheduler';
import { jestMatcher } from '../utils';
import { Subscription, SchedulerAction, merge } from 'rxjs';
import { delay } from 'rxjs/operators';
import { AsapAction } from '../../src/lib/scheduler/asap/AsapAction';
import { AsapScheduler } from '../../src/lib/scheduler/asap/AsapScheduler';
import { asapScheduler } from '../../src/lib/scheduler/asap/asap';

let asap: AsapScheduler;
const asap: AsapScheduler = asapScheduler;

/** @test {Scheduler} */
describe('Scheduler.asap', () => {
let testScheduler: RxTestScheduler;
const proxyZone = window['Zone']['ProxyZoneSpec'];

beforeEach(() => {
testScheduler = new RxTestScheduler(jestMatcher);
asap = new AsapScheduler(AsapAction);
window['Zone']['ProxyZoneSpec'] = undefined;
jest.clearAllTimers();
jest.useRealTimers();
jest.clearAllMocks();
});

afterEach(() => {
window['Zone']['ProxyZoneSpec'] = proxyZone;
});

it('should exist', () => {
expect(asap).toBeDefined();
});
Expand Down Expand Up @@ -66,6 +71,10 @@ describe('Scheduler.asap', () => {
jest.useFakeTimers();
// callThrough is missing from the declarations installed by the typings tool in stable
const intervalSpy = jest.spyOn(intervalProvider, 'setInterval');
intervalProvider.delegate = {
setInterval: setInterval,
clearInterval: clearInterval,
};
const period = 50;
const state = { index: 0, period };
type State = typeof state;
Expand All @@ -80,17 +89,22 @@ describe('Scheduler.asap', () => {
expect(state).toHaveProperty('index', 0);
expect(intervalSpy).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(period);
// expect(state).toHaveProperty('index', 1);
expect(state).toHaveProperty('index', 1);
expect(intervalSpy).toHaveBeenCalledTimes(2);
jest.advanceTimersByTime(period);
expect(state).toHaveProperty('index', 2);
expect(intervalSpy).toHaveBeenCalledTimes(3);
intervalProvider.delegate = undefined;
});

it('should reuse the interval for recursively scheduled actions with the same delay', () => {
jest.useFakeTimers();
// callThrough is missing from the declarations installed by the typings tool in stable
const intervalSpy = jest.spyOn(intervalProvider, 'setInterval');
intervalProvider.delegate = {
setInterval: setInterval,
clearInterval: clearInterval,
};
const period = 50;
const state = { index: 0, period };
type State = typeof state;
Expand All @@ -103,12 +117,13 @@ describe('Scheduler.asap', () => {
asap.schedule(dispatch as any, period, state);
expect(state).toHaveProperty('index', 0);
expect(intervalSpy).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(period + 1);
jest.advanceTimersByTime(period);
expect(state).toHaveProperty('index', 1);
expect(intervalSpy).toHaveBeenCalledTimes(1);
jest.advanceTimersByTime(period);
expect(state).toHaveProperty('index', 2);
expect(intervalSpy).toHaveBeenCalledTimes(1);
intervalProvider.delegate = undefined;
});

it('should schedule an action to happen later', (done) => {
Expand Down
12 changes: 9 additions & 3 deletions packages/rxjs-zone-less/spec/scheduler/queueScheduler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Subscription, merge } from 'rxjs';
import { delay } from 'rxjs/operators';
import { intervalProvider } from '../../src/lib/internals/intervalProvider';
import { queueScheduler } from '../../src/lib/scheduler/queue/queue';
import { RxTestScheduler } from '../../src/lib/testing/test-scheduler';
import { jestMatcher } from '../utils';
Expand All @@ -12,10 +13,9 @@ describe('Scheduler.queue', () => {

beforeEach(() => {
testScheduler = new RxTestScheduler(jestMatcher);
});

afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
jest.clearAllMocks();
});

it('should act like the async scheduler if delay > 0', () => {
Expand All @@ -33,6 +33,10 @@ describe('Scheduler.queue', () => {

it('should switch from synchronous to asynchronous at will', () => {
jest.useFakeTimers();
intervalProvider.delegate = {
setInterval: setInterval,
clearInterval: clearInterval,
};

let asyncExec = false;
const state: Array<number> = [];
Expand All @@ -58,6 +62,7 @@ describe('Scheduler.queue', () => {

expect(asyncExec).toBe(true);
expect(state).toEqual([0, 1, 2]);
intervalProvider.delegate = undefined;
});

it('should unsubscribe the rest of the scheduled actions if an action throws an error', () => {
Expand Down Expand Up @@ -89,3 +94,4 @@ describe('Scheduler.queue', () => {
expect(errorValue.message).toEqual('oops');
});
});

1 change: 1 addition & 0 deletions packages/rxjs-zone-less/spec/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'jest-preset-angular/setup-jest';
3 changes: 0 additions & 3 deletions packages/rxjs-zone-less/spec/utils.d.ts

This file was deleted.

53 changes: 0 additions & 53 deletions packages/rxjs-zone-less/spec/utils.js

This file was deleted.

17 changes: 9 additions & 8 deletions packages/rxjs-zone-less/src/lib/internals/zone-less.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

// copied from @angular/core
declare let global: any
declare let WorkerGlobalScope: any
declare let global: any;
declare let WorkerGlobalScope: any;

const __globalThis = typeof globalThis !== 'undefined' && globalThis;
const __window = typeof window !== 'undefined' && window;
const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope && self;
const __self =
typeof self !== 'undefined' &&
typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope &&
self;
const __global = typeof global !== 'undefined' && global;

// Always use __globalThis if available, which is the spec-defined global variable across all
Expand Down Expand Up @@ -35,7 +37,7 @@ export function clearInterval(id: number): void {
*/
export function getZoneUnPatchedApi<
N extends keyof (Window & typeof globalThis)
>(name: N): (Window & typeof globalThis)[N];
>(name: N): (Window & typeof globalThis)[N];

export function getZoneUnPatchedApi<T extends object, N extends keyof T>(
target: T,
Expand All @@ -53,6 +55,5 @@ export function getZoneUnPatchedApi<T extends object, N extends keyof T>(
name = targetOrName as N;
targetOrName = _global as T;
}
return targetOrName['__zone_symbol__' + name] || targetOrName[name];
return targetOrName[`__zone_symbol__${String(name)}`] || targetOrName[name];
}

1 change: 1 addition & 0 deletions packages/rxjs-zone-less/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["spec/test-setup.ts"],
"include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"]
}