angular/packages/zone.js/test/browser/MutationObserver.spec.ts
Paul Gschwendtner 6fed377140 test: update tests to not run jasmine done function in sync-test zone from describe block
There are some ZoneJS tests that fork the zone from the `describe` block
for testing the zone patching. This does cause the Jasmine `done`
function later in `it` specs to be invoked in the sync-test zone from
the original `describe` block. The `done` implementation now has
changed with the Karma Jasmine update and breaks because it now causes
tasks to be scheduled.

It is conceptually incorrect/invalid to take the describe sync zone and
run test logic with that sync zone.

```
 An error was thrown in afterAll
  error properties: Object({ originalStack: 'Error: Cannot call jasmine.execute().forceTask from within a sync test (syncTestZone for jasmine.describe#FileReader).
      at new ZoneAwareError (packages/zone.js/test/browser_test_rollup.umd.js:98:37)
      at e.onScheduleTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:158:196)
      at e.scheduleTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:14:7529)
      at t.scheduleTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:14:3539)
      at t.scheduleMicroTask (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:14:3791)
      at r.execute (packages/zone.js/bundles/zone-testing-bundle.umd.min.js:166:4312)
      at queueRunnerFa ...
      at <Jasmine>
```
2022-07-18 19:19:00 +02:00

74 lines
2.2 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ifEnvSupports} from '../test-util';
declare const global: any;
describe('MutationObserver', ifEnvSupports('MutationObserver', function() {
let elt: HTMLDivElement;
beforeEach(function() {
elt = document.createElement('div');
});
it('should run observers within the zone', function(done) {
const testZone = Zone.current.fork({name: 'test'});
let ob;
testZone.run(function() {
ob = new MutationObserver(function() {
expect(Zone.current).toBe(testZone);
done();
});
ob.observe(elt, {childList: true});
});
elt.innerHTML = '<p>hey</p>';
});
it('should only dequeue upon disconnect if something is observed', function() {
let ob: MutationObserver;
let flag = false;
const elt = document.createElement('div');
const childZone = Zone.current.fork({
name: 'test',
onInvokeTask: function() {
flag = true;
}
});
childZone.run(function() {
ob = new MutationObserver(function() {});
});
ob!.disconnect();
expect(flag).toBe(false);
});
}));
describe('WebKitMutationObserver', ifEnvSupports('WebKitMutationObserver', function() {
it('should run observers within the zone', function(done) {
const testZone = Zone.current.fork({name: 'test'});
let elt: HTMLDivElement;
testZone.run(function() {
elt = document.createElement('div');
const ob = new global['WebKitMutationObserver'](function() {
expect(Zone.current).toBe(testZone);
done();
});
ob.observe(elt, {childList: true});
});
elt!.innerHTML = '<p>hey</p>';
});
}));