This Ember CLI addon provides the window
global as an ES6 module import that you can use in any component or controller whereyou need window
. But some of its properties and functions are prohibitive to be usedin tests as they will break the test run:
window.location.href
to trigger a redirect, as that will leave your test pagealert
, confirm
and prompt
are blocking calls, and cannot be closed without user interaction, so they will justsuspend your test runSo when running tests this import will be replaced with one that mocks these critical parts.
ember install ember-window-mock
Let's say you want to redirect to an external URL. A simple controller could look like this:
import Controller from '@ember/controller';
import { action } from '@ember/object';
export default class IndexController extends Controller {
@action
redirect(url) {
window.location.href = url;
}
}
With this addon, you can just import window
instead of using the global:
import Controller from '@ember/controller';
import { action } from '@ember/object';
import window from 'ember-window-mock';
export default class IndexController extends Controller {
@action
redirect(url) {
window.location.href = url;
}
}
Everything else works as you would expect, since the import is exactly the same as the global, when not running tests.
When running in the test environment, the import will be replaced with a mock. It is a proxy to window
, so all of thenon-critical properties and functions just use the normal window
global. But the critical parts are replaced suitablefor tests:
window.location
is mocked with an object with the same API (members like .href
or .host
), but settinglocation.href
will just do nothing. Still reading from location.href
will return the value that was previously set,so you can run assertions against that value to check if you app tried to redirect to the expected URL.window.localStorage
is also mocked with an object with the same API (getItem
, setItem
, removeItem
, clear
, key
, and length
). Storage is not persistent and does not affect your browser's localStorage
object.alert
, confirm
and prompt
are replaced by simple noop functions (they do nothing). You can use a mocking librarylike Sinon.js to replace them with spies or stubs to assert that they have been called or toreturn some predefined value (e.g. true
for confirm
).Moreover it allows you to set any (nested) properties, even if they are defined as read only. This way you can pretenddifferent environments in your tests. For example you can fake different devices by changing
window.navigator.userAgent
when you do user agent detection in your app.window.screen.width
to test responsive layouts when your components render differently based on it.See below for some examples.
Important:
Proxy
, so your development environment and tests need to run in a browser like Chrome thatsupports Proxy
natively (as it cannot be transpiled by Babel)It is possible to leak some state on the window mock between tests. For example when your app sets location.href
in atest like this:
window.location.href = 'http://www.example.com';
For the following test window.location.href
will still be 'http://www.example.com'
, but instead it should have afresh instance of the window mock. Therefore this addon exports a setupWindowMock
function to kill all changed state on window
:
import { setupWindowMock } from 'ember-window-mock/test-support';
module('SidebarController', function(hooks) {
setupWindowMock(hooks);
test(...);
});
window.location
Given a controller like the one above, that redirects to some URL when a button is clicked, an application test could like this:
import { module, test } from 'qunit';
import { click, visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import window from 'ember-window-mock';
import { setupWindowMock } from 'ember-window-mock/test-support';
module('Acceptance | redirect', function(hooks) {
setupApplicationTest(hooks);
setupWindowMock(hooks);
test('it redirects when clicking the button', async function(assert) {
await visit('/');
await click('button');
assert.equal(window.location.href, 'http://www.example.com');
});
});
confirm()
Here is an example that uses ember-sinon-qunit to replace confirm
,so you can easily check if it has been called, and to return some defined value:
import { module, test } from 'qunit';
import { click, visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import window from 'ember-window-mock';
import { setupWindowMock } from 'ember-window-mock/test-support';
import sinon from 'sinon';
module('Acceptance | redirect', function(hooks) {
setupApplicationTest(hooks);
setupWindowMock(hooks);
test('it deletes an item', async function(assert) {
let stub = sinon.stub(window, 'confirm');
stub.returns(true);
await visit('/');
await click('[data-test-delete]');
assert.ok(stub.calledOnce);
assert.ok(stub.calledWith('Are you sure?'));
});
});
open()
Here is an example that assigns a new function to replace open
.You can check if the function has been called as well as the value of its parameters.
import { module, test } from 'qunit';
import { click, visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import window from 'ember-window-mock';
import { setupWindowMock } from 'ember-window-mock/test-support';
module('Acceptance | new window', function(hooks) {
setupApplicationTest(hooks);
setupWindowMock(hooks);
test('it opens a new window when clicking the button', async function(assert) {
await visit('/');
window.open = (urlToOpen) => {
assert.equal(urlToOpen, 'http://www.example.com/some-file.jpg');
};
await click('button');
});
});
See the Contributing guide for details.
This project is licensed under the MIT License.
介绍 (Introduction) 类Window是一个顶级窗口,没有边框,没有菜单栏。 它使用BorderLayout作为默认布局管理器。 类声明 以下是java.awt.Window类的声明: public class Window extends Container implements Accessible 类构造函数 SN 构造函数和描述 1 Window(Frame
此UI组件用于创建一个窗口,该窗口应在发生任何事件时弹出。 窗口基本上是一个面板,当按钮/链接点击或悬停在任何事件时,它应该出现。 语法 (Syntax) 以下是创建窗口的简单语法。 win = new Ext.Window({ properties }); 例子 (Example) 以下是显示电子邮件窗口的简单示例。 <!DOCTYPE html> <html> <head>
介绍 (Introduction) JWindow类是一个可以显示但没有标题栏或窗口管理按钮的容器。 Class 声明 (Class Declaration) 以下是javax.swing.JWindow类的声明 - public class JWindow extends Window implements Accessible, RootPaneContainer 字段 (
window 将 Observable 分解为多个子 Observable,周期性的将子 Observable 发出来 window 操作符和 buffer 十分相似,buffer 周期性的将缓存的元素集合发送出来,而 window 周期性的将元素集合以 Observable 的形态发送出来。 buffer 要等到元素搜集完毕后,才会发出元素序列。而 window 可以实时发出元素序列。
window 函数签名: window(windowBoundaries: Observable): Observable 时间窗口值的 observable 。 示例 示例 1: 打开由内部 observable 指定的窗口 ( StackBlitz | jsBin | jsFiddle ) // RxJS v6+ import { timer, interval } from 'rxjs';
主要内容:一、Window 对象,二、Window对象的方法浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器"对话"。 一、Window 对象 所有浏览器都支持 window 对象。它表示浏览器窗口。 所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。 全局变量是 window 对象的属性。 全局函数是 window 对象的方法。 甚至 HTML DOM 的 document 也是 window 对