diff --git a/intern.json b/intern.json index 26a41dc74..016f39ec9 100644 --- a/intern.json +++ b/intern.json @@ -36,7 +36,8 @@ { "script": "./dist/dev/tests/widget-core/support/jsdom-plugin.js", "useLoader": true - } + }, + "./dist/dev/tests/shim/support/has-plugin.js" ], "browser": { @@ -51,6 +52,10 @@ "name": "immutable", "location": "./node_modules/immutable/dist", "main": "immutable.js" + }, + { + "name": "cross-fetch", + "location": "./node_modules/cross-fetch" } ], "map": { @@ -65,7 +70,7 @@ "./dist/dev/tests/widget-core/unit/all.js" ], "plugins+": [ - "./node_modules/whatwg-fetch/dist/fetch.umd.js", + "./node_modules/cross-fetch/dist/browser-polyfill.js", { "script": "./dist/dev/src/shim/browser.js", "useLoader": true diff --git a/package.json b/package.json index c861753c1..b4f83cbc3 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "@types/globalize": "0.0.34", "@webcomponents/webcomponentsjs": "1.1.0", "cldrjs": "0.5.0", + "cross-fetch": "3.0.2", "css-select-umd": "1.3.0-rc0", "diff": "3.5.0", "globalize": "1.4.0", @@ -53,8 +54,7 @@ "pepjs": "0.4.2", "resize-observer-polyfill": "1.5.0", "tslib": "1.8.1", - "web-animations-js": "2.3.1", - "whatwg-fetch": "3.0.0" + "web-animations-js": "2.3.1" }, "devDependencies": { "@dojo/loader": "^2.0.0", diff --git a/src/has/has.ts b/src/has/has.ts index 33068e523..a61f5c2ec 100644 --- a/src/has/has.ts +++ b/src/has/has.ts @@ -486,3 +486,5 @@ add('dom-resize-observer', () => has('host-browser') && global.ResizeObserver != add('dom-pointer-events', () => has('host-browser') && global.onpointerdown !== undefined, true); add('build-elide', false); + +add('test', false); diff --git a/src/shim/IntersectionObserver.ts b/src/shim/IntersectionObserver.ts index 26e001dff..69bbdccb5 100644 --- a/src/shim/IntersectionObserver.ts +++ b/src/shim/IntersectionObserver.ts @@ -1,5 +1,5 @@ -import global from './global'; `!has('build-elide')`; import 'intersection-observer'; +import wrapper from './util/wrapper'; -export default global.IntersectionObserver as typeof IntersectionObserver; +export default wrapper('IntersectionObserver', true) as typeof IntersectionObserver; diff --git a/src/shim/ResizeObserver.ts b/src/shim/ResizeObserver.ts index 1c7690a51..3e5aa4cb1 100644 --- a/src/shim/ResizeObserver.ts +++ b/src/shim/ResizeObserver.ts @@ -2,6 +2,7 @@ import global from './global'; import has from '../has/has'; `!has('build-elide')`; import * as Resize from 'resize-observer-polyfill'; +import wrapper from './util/wrapper'; export interface DOMRectReadOnly { readonly x: number; @@ -24,13 +25,16 @@ export interface ResizeObserverEntry { } export interface ResizeObserver { - prototype: ResizeObserver; - new (callback: ResizeObserverCallback): ResizeObserver; observe(target: Element): void; unobserve(target: Element): void; disconnect(): void; } +declare var ResizeObserver: { + prototype: ResizeObserver; + new (callback: ResizeObserverCallback): ResizeObserver; +}; + if (!has('build-elide')) { if (!global.ResizeObserver) { // default is undefined when UMD module is used @@ -38,4 +42,4 @@ if (!has('build-elide')) { } } -export default global.ResizeObserver as ResizeObserver; +export default wrapper('ResizeObserver', true) as typeof ResizeObserver; diff --git a/src/shim/WebAnimations.ts b/src/shim/WebAnimations.ts index 1164e9f57..75c149e5f 100644 --- a/src/shim/WebAnimations.ts +++ b/src/shim/WebAnimations.ts @@ -1,6 +1,6 @@ -import global from './global'; `!has('build-elide')`; import 'web-animations-js/web-animations-next-lite.min'; +import wrapper from './util/wrapper'; export type AnimationEffectTimingFillMode = 'none' | 'forwards' | 'backwards' | 'both' | 'auto'; export type AnimationEffectTimingPlaybackDirection = 'normal' | 'reverse' | 'alternate' | 'alternate-reverse'; @@ -116,5 +116,5 @@ export interface AnimationConstructor { new (effect?: AnimationEffectReadOnly, timeline?: AnimationTimeline): Animation; } -export const Animation = global.Animation as AnimationConstructor; -export const KeyframeEffect = global.KeyframeEffect as KeyframeEffectConstructor; +export const Animation = wrapper('Animation', true) as AnimationConstructor; +export const KeyframeEffect = wrapper('KeyframeEffect', true) as KeyframeEffectConstructor; diff --git a/src/shim/fetch.ts b/src/shim/fetch.ts index 88446a8c5..b54b7fc27 100644 --- a/src/shim/fetch.ts +++ b/src/shim/fetch.ts @@ -1,5 +1,5 @@ -import global from './global'; `!has('build-elide')`; -import 'whatwg-fetch'; +import 'cross-fetch/polyfill'; +import wrapper from './util/wrapper'; -export default global.fetch.bind(global) as (input: RequestInfo, init?: RequestInit) => Promise; +export default wrapper('fetch', false, true) as (input: RequestInfo, init?: RequestInit) => Promise; diff --git a/src/shim/util/wrapper.ts b/src/shim/util/wrapper.ts new file mode 100644 index 000000000..cde91218d --- /dev/null +++ b/src/shim/util/wrapper.ts @@ -0,0 +1,18 @@ +import global from '../global'; +import has from '../../has/has'; + +export default function wrapper(nameOnGlobal: string, constructor = false, bind = false): any { + if (has('test')) { + if (constructor) { + return function(...args: any[]) { + return new global[nameOnGlobal](...args); + }; + } else { + return function(...args: any[]) { + return global[nameOnGlobal](...args); + }; + } + } + + return bind ? global[nameOnGlobal].bind(global) : global[nameOnGlobal]; +} diff --git a/tests/shim/support/has-plugin.ts b/tests/shim/support/has-plugin.ts new file mode 100644 index 000000000..6fdb07a5a --- /dev/null +++ b/tests/shim/support/has-plugin.ts @@ -0,0 +1,18 @@ +intern.registerPlugin('has', () => { + const DojoHasEnvironment = { + staticFeatures: { + test: true + } + }; + if (typeof global !== 'undefined') { + (global as any).DojoHasEnvironment = DojoHasEnvironment; + } + + if (typeof self !== 'undefined') { + (self as any).DojoHasEnvironment = DojoHasEnvironment; + } + + if (typeof window !== 'undefined') { + (window as any).DojoHasEnvironment = DojoHasEnvironment; + } +}); diff --git a/tests/shim/support/util.ts b/tests/shim/support/util.ts index 487ad9069..b6ed34d68 100644 --- a/tests/shim/support/util.ts +++ b/tests/shim/support/util.ts @@ -9,6 +9,6 @@ export function isEventuallyRejected(promise: PromiseLike): PromiseLike void; let bindInstance: WidgetBase; let isFoo: SinonStub; let isBar: SinonStub; -let Resize: any; let observer: { observe: SinonStub; disconnect: SinonStub; }; +let globalResizeObserver: any; registerSuite('meta - Resize', { async before() { bindInstance = new WidgetBase(); @@ -24,8 +25,8 @@ registerSuite('meta - Resize', { resizeCallback = callback; return observer; }); + globalResizeObserver = global.ResizeObserver; global.ResizeObserver = resizeObserver; - Resize = (await import('../../../../src/widget-core/meta/Resize')).default; }, beforeEach() { @@ -43,6 +44,10 @@ registerSuite('meta - Resize', { resizeObserver.resetHistory(); }, + after() { + global.ResizeObserver = globalResizeObserver; + }, + tests: { 'Will return predicates defaulted to false if node not loaded'() { const nodeHandler = new NodeHandler(); diff --git a/tests/widget-core/unit/meta/WebAnimation.ts b/tests/widget-core/unit/meta/WebAnimation.ts index a83d5891b..c70c8e55d 100644 --- a/tests/widget-core/unit/meta/WebAnimation.ts +++ b/tests/widget-core/unit/meta/WebAnimation.ts @@ -1,14 +1,16 @@ import global from '../../../../src/shim/global'; const { assert } = intern.getPlugin('chai'); -const { afterEach, beforeEach, before, describe, it } = intern.getInterface('bdd'); +const { after, afterEach, beforeEach, before, describe, it } = intern.getInterface('bdd'); const { describe: jsdomDescribe } = intern.getPlugin('jsdom'); import { AnimationEffectTiming } from '../../../../src/shim/WebAnimations'; +import WebAnimation from '../../../../src/widget-core/meta/WebAnimation'; import { WidgetBase } from '../../../../src/widget-core/WidgetBase'; import { v } from '../../../../src/widget-core/d'; import { spy, stub } from 'sinon'; -let WebAnimation: any; let animationExists = false; +let globalAnimation: any; +let globalKeyframeEffect: any; jsdomDescribe('WebAnimation', () => { let effects: any; @@ -52,12 +54,14 @@ jsdomDescribe('WebAnimation', () => { let metaStub: any; before(async () => { - class KeyframeEffectMock { + globalAnimation = global.Animation; + globalKeyframeEffect = global.KeyframeEffect; + global.KeyframeEffect = class { constructor(...args: any[]) { keyframeCtorStub(...args); } - } - class AnimationMock { + }; + global.Animation = class { constructor(...args: any[]) { animationCtorStub(...args); } @@ -103,10 +107,12 @@ jsdomDescribe('WebAnimation', () => { set playbackRate(rate: number) { playbackRateStub(rate); } - } - global.KeyframeEffect = KeyframeEffectMock; - global.Animation = AnimationMock; - WebAnimation = (await import('../../../../src/widget-core/meta/WebAnimation')).default; + }; + }); + + after(() => { + global.KeyframeEffect = globalKeyframeEffect; + global.Animation = globalAnimation; }); beforeEach(() => {