mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-14 12:09:04 +08:00
Jasmine-jQuery: added tests
This commit is contained in:
135
jasmine-jquery/jasmine-jquery-tests.ts
Normal file
135
jasmine-jquery/jasmine-jquery-tests.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
/// <reference path="../jasmine/jasmine.d.ts" />
|
||||
/// <reference path="../jquery/jquery.d.ts" />
|
||||
/// <reference path="jasmine-jquery.d.ts" />
|
||||
|
||||
describe("Jasmine jQuery extension", () => {
|
||||
it("Adds jQuery matchers", () => {
|
||||
expect($('<div id="some-id"></div>')).toBe('div');
|
||||
expect($('<div id="some-id"></div>')).toBe('div#some-id');
|
||||
expect($('<input type="checkbox" checked="checked"/>')).toBeChecked();
|
||||
expect($('<div id="some-id"></div>')).toBeHidden();
|
||||
expect($('<div style="display: none; margin: 10px;"></div>')).toHaveCss({ display: "none", margin: "10px" });
|
||||
expect($('<div style="display: none; margin: 10px;"></div>')).toHaveCss({ margin: "10px" });
|
||||
expect($('<option selected="selected"></option>')).toBeSelected();
|
||||
expect($('<div id="some-id"></div>')).toBeVisible();
|
||||
expect($('<div><span class="some-class"></span></div>')).toContain('span.some-class');
|
||||
expect($('<span></span>').addClass('js-something')).toBeMatchedBy('.js-something');
|
||||
expect($('<span></span>')).toExist();
|
||||
expect($('<div id="some-id"></div>')).toHaveAttr('id', 'some-id');
|
||||
expect($('<div id="some-id"></div>')).toHaveProp('id', 'some-id');
|
||||
expect($('')).toHaveBeenTriggered();
|
||||
expect($('')).toHaveBeenTriggeredOn('#some-id');
|
||||
expect($('')).toHaveBeenTriggeredOnAndWith('#some-id', 'eventParam');
|
||||
expect($('')).toHaveBeenPrevented();
|
||||
expect($('')).toHaveBeenPreventedOn('#some-id');
|
||||
expect($('')).toHaveBeenStopped();
|
||||
expect($('')).toHaveBeenStoppedOn('#some-id');
|
||||
expect($('<div class="some-class"></div>')).toHaveClass("some-class");
|
||||
expect($('<div data-item="value"></div>')).toHaveData('item', 'value');
|
||||
expect($('<div><span></span></div>')).toHaveHtml('<span></span>');
|
||||
expect($('<div><ul></ul><h1>header</h1></div>')).toContainHtml('<ul></ul>');
|
||||
expect($('<div><ul></ul><h1>header</h1></div>')).toContainText('header');
|
||||
expect($('<div id="some-id"></div>')).toHaveId("some-id");
|
||||
expect($('<div>some text</div>')).toHaveText('some text');
|
||||
expect($('<input type="text" value="some text"/>')).toHaveValue('some text');
|
||||
expect($('ul > li')).toHaveLength(3);
|
||||
expect($('<input type="submit" disabled ="disabled"/>')).toBeDisabled();
|
||||
expect($('<input type="text" />').focus()).toBeFocused();
|
||||
//expect($form).toHandle("submit")
|
||||
//expect($form).toHandleWith("submit", yourSubmitCallback)
|
||||
});
|
||||
|
||||
it("Handles HTML Fixtures", () => {
|
||||
jasmine.getFixtures().fixturesPath = 'my/new/path';
|
||||
jasmine.getFixtures().containerId = 'my-new-id';
|
||||
|
||||
jasmine.getFixtures().load('myfixture.html');
|
||||
jasmine.getFixtures().appendLoad('myfixture.html', 'myfixture2.html');
|
||||
jasmine.getFixtures().read('myfixture.html', 'myfixture2.html');
|
||||
jasmine.getFixtures().set('<html></html>');
|
||||
jasmine.getFixtures().appendSet('<html></html>');
|
||||
jasmine.getFixtures().preload('myfixture.html', 'myfixture2.html');
|
||||
jasmine.getFixtures().clearCache();
|
||||
jasmine.getFixtures().cleanUp();
|
||||
|
||||
loadFixtures('myfixture.html');
|
||||
appendLoadFixtures('myfixture.html');
|
||||
readFixtures('myfixture.html');
|
||||
setFixtures('<html></html>');
|
||||
appendSetFixtures('<html></html>');
|
||||
|
||||
sandbox();
|
||||
sandbox({
|
||||
id: 'my-id',
|
||||
class: 'my-class',
|
||||
myattr: 'my-attr'
|
||||
});
|
||||
|
||||
setFixtures(sandbox({ class: 'my-class' }));
|
||||
});
|
||||
|
||||
it("Handles Style Fixtures", () => {
|
||||
jasmine.getStyleFixtures().fixturesPath = 'my/new/path';
|
||||
|
||||
jasmine.getStyleFixtures().load('myfixture.css');
|
||||
jasmine.getStyleFixtures().appendLoad('myfixture.css', 'myfixture2.css');
|
||||
jasmine.getStyleFixtures().set('.elem { position: absolute }');
|
||||
jasmine.getStyleFixtures().appendSet('.elem { position: absolute }');
|
||||
jasmine.getStyleFixtures().preload('myfixture.css', 'myfixture2.css');
|
||||
jasmine.getStyleFixtures().clearCache();
|
||||
jasmine.getStyleFixtures().cleanUp();
|
||||
|
||||
loadStyleFixtures('myfixture.css');
|
||||
appendLoadFixtures('myfixture.css');
|
||||
setStyleFixtures('.elem { position: absolute }');
|
||||
appendSetStyleFixtures('.elem { position: absolute }');
|
||||
});
|
||||
|
||||
it("Handles JSON Fixtures", () => {
|
||||
jasmine.getJSONFixtures().fixturesPath = 'my/new/path';
|
||||
|
||||
jasmine.getJSONFixtures().load('myfixture.json');
|
||||
jasmine.getJSONFixtures().read('myfixture.json');
|
||||
jasmine.getJSONFixtures().clearCache();
|
||||
|
||||
var data = getJSONFixture('myjsonfixture.json');
|
||||
var fixtures = loadJSONFixtures('myjsonfixture.json');
|
||||
var data = fixtures['myjsonfixture.json'];
|
||||
});
|
||||
|
||||
describe("Event Spies", () => {
|
||||
it("First, spy on the event", () => {
|
||||
var spyEvent = spyOnEvent('#some_element', 'click');
|
||||
$('#some_element').click();
|
||||
expect('click').toHaveBeenTriggeredOn('#some_element');
|
||||
expect(spyEvent).toHaveBeenTriggered();
|
||||
});
|
||||
|
||||
it("You can reset spy events", () => {
|
||||
var spyEvent = spyOnEvent('#some_element', 'click');
|
||||
$('#some_element').click();
|
||||
expect('click').toHaveBeenTriggeredOn('#some_element');
|
||||
expect(spyEvent).toHaveBeenTriggered();
|
||||
// reset spy events
|
||||
spyEvent.reset();
|
||||
expect('click').not.toHaveBeenTriggeredOn('#some_element');
|
||||
expect(spyEvent).not.toHaveBeenTriggered();
|
||||
});
|
||||
|
||||
it("You can similarly check if triggered event was prevented", () => {
|
||||
var spyEvent = spyOnEvent('#some_element', 'click');
|
||||
$('#some_element').click(function (event) { event.preventDefault(); });
|
||||
$('#some_element').click();
|
||||
expect('click').toHaveBeenPreventedOn('#some_element');
|
||||
expect(spyEvent).toHaveBeenPrevented();
|
||||
});
|
||||
|
||||
it("You can also check if the triggered event was stopped", () => {
|
||||
var spyEvent = spyOnEvent('#some_element', 'click');
|
||||
$('#some_element').click(function (event) { event.stopPropagation(); });
|
||||
$('#some_element').click();
|
||||
expect('click').toHaveBeenStoppedOn('#some_element');
|
||||
expect(spyEvent).toHaveBeenStopped();
|
||||
});
|
||||
});
|
||||
})
|
||||
59
jasmine-jquery/jasmine-jquery.d.ts
vendored
59
jasmine-jquery/jasmine-jquery.d.ts
vendored
@@ -5,22 +5,26 @@
|
||||
|
||||
/// <reference path="../jquery/jquery.d.ts"/>
|
||||
|
||||
declare function sandbox(attributes?: any): string;
|
||||
|
||||
declare function readFixtures(...uls: string[]): string;
|
||||
declare function preloadFixtures(...uls: string[]);
|
||||
declare function loadFixtures(...uls: string[]);
|
||||
declare function appendLoadFixtures(...uls: string[]);
|
||||
declare function setFixtures(html: string): string;
|
||||
declare function appendSetFixtures();
|
||||
declare function sandbox(attributes): JQuery;
|
||||
declare function spyOnEvent(selector: JQuery, eventName: string): any;
|
||||
declare function preloadStyleFixtures();
|
||||
declare function loadStyleFixtures();
|
||||
declare function appendLoadStyleFixtures();
|
||||
declare function appendSetFixtures(html: string);
|
||||
|
||||
declare function preloadStyleFixtures(...uls: string[]);
|
||||
declare function loadStyleFixtures(...uls: string[]);
|
||||
declare function appendLoadStyleFixtures(...uls: string[]);
|
||||
declare function setStyleFixtures(html: string);
|
||||
declare function appendSetStyleFixtures(html: string);
|
||||
declare function loadJSONFixtures(): jasmine.JSONFixtures;
|
||||
|
||||
declare function loadJSONFixtures(...uls: string[]): jasmine.JSONFixtures;
|
||||
declare function getJSONFixture(url: string): any;
|
||||
|
||||
declare function spyOnEvent(selector: string, eventName: string): jasmine.JQueryEventSpy;
|
||||
|
||||
declare module jasmine {
|
||||
function spiedEventsKey(selector: JQuery, eventName: string): string;
|
||||
|
||||
@@ -30,6 +34,7 @@ declare module jasmine {
|
||||
|
||||
interface Fixtures {
|
||||
fixturesPath: string;
|
||||
containerId: string;
|
||||
set(html: string): string;
|
||||
appendSet(html: string);
|
||||
preload(...uls: string[]);
|
||||
@@ -38,16 +43,17 @@ declare module jasmine {
|
||||
read(...uls: string[]): string;
|
||||
clearCache();
|
||||
cleanUp();
|
||||
sandbox(attributes): JQuery;
|
||||
sandbox(attributes?: any): string;
|
||||
createContainer_(html: string);
|
||||
addToContainer_(html: string);
|
||||
getFixtureHtml_(url: string): string;
|
||||
loadFixtureIntoCache_(relativeUrl: string);
|
||||
makeFixtureUrl_(relativeUrl: string): string;
|
||||
proxyCallTo_(methodName, passedArguments): any;
|
||||
proxyCallTo_(methodName: string, passedArguments): any;
|
||||
}
|
||||
|
||||
interface StyleFixtures {
|
||||
fixturesPath: string;
|
||||
set(html: string): string;
|
||||
appendSet(html: string);
|
||||
preload(...uls: string[]);
|
||||
@@ -60,22 +66,19 @@ declare module jasmine {
|
||||
getFixtureHtml_(url: string): string;
|
||||
loadFixtureIntoCache_(relativeUrl: string);
|
||||
makeFixtureUrl_(relativeUrl: string): string;
|
||||
proxyCallTo_(methodName, passedArguments): any;
|
||||
proxyCallTo_(methodName: string, passedArguments): any;
|
||||
}
|
||||
|
||||
interface JSONFixtures {
|
||||
fixturesPath: string;
|
||||
load(...uls: string[]);
|
||||
read(...uls: string[]): string;
|
||||
clearCache();
|
||||
getFixtureData_(url: string): any;
|
||||
loadFixtureIntoCache_(relativeUrl: string);
|
||||
proxyCallTo_(methodName, passedArguments): any;
|
||||
proxyCallTo_(methodName: string, passedArguments): any;
|
||||
}
|
||||
|
||||
var Fixtures: Fixtures;
|
||||
var StyleFixtures: StyleFixtures;
|
||||
var JSONFixtures: JSONFixtures;
|
||||
|
||||
interface Matchers {
|
||||
toHaveClass(className: string): boolean;
|
||||
toHaveCss(css): boolean;
|
||||
@@ -104,18 +107,36 @@ declare module jasmine {
|
||||
toHandleWith(eventName: string, eventHandler): boolean;
|
||||
|
||||
toHaveBeenTriggered(): boolean;
|
||||
toHaveBeenTriggeredOn(selector: JQuery): boolean;
|
||||
toHaveBeenTriggeredOnAndWith(selector: JQuery, ...args: any[]): boolean;
|
||||
toHaveBeenTriggeredOn(selector: string): boolean;
|
||||
toHaveBeenTriggeredOnAndWith(selector: string, ...args: any[]): boolean;
|
||||
toHaveBeenPrevented(): boolean;
|
||||
toHaveBeenPreventedOn(selector: JQuery): boolean;
|
||||
toHaveBeenPreventedOn(selector: string): boolean;
|
||||
toHaveBeenStopped(): boolean;
|
||||
toHaveBeenStoppedOn(selector: JQuery): boolean;
|
||||
toHaveBeenStoppedOn(selector: string): boolean;
|
||||
}
|
||||
|
||||
interface JQueryEventSpy {
|
||||
selector: string;
|
||||
eventName: string;
|
||||
handler(eventObject: JQueryEventObject): any;
|
||||
reset(): any;
|
||||
}
|
||||
|
||||
interface JasmineJQuery {
|
||||
browserTagCaseIndependentHtml(html: string): string;
|
||||
elementToString(element: JQuery): string;
|
||||
matchersClass: any;
|
||||
events: JasmineJQueryEvents;
|
||||
}
|
||||
|
||||
interface JasmineJQueryEvents {
|
||||
spyOn(selector: string, eventName: string): JQueryEventSpy;
|
||||
args(selector: string, eventName: string): any;
|
||||
wasTriggered(selector: string, eventName: string): boolean;
|
||||
wasTriggeredWith(selector: string, eventName: string, expectedArgs: any, env: jasmine.Env): boolean;
|
||||
wasPrevented(selector: string, eventName: string): boolean;
|
||||
wasStopped(selector: string, eventName: string): boolean;
|
||||
cleanUp();
|
||||
}
|
||||
|
||||
var JQuery: JasmineJQuery;
|
||||
|
||||
Reference in New Issue
Block a user