diff --git a/jasmine-jquery/jasmine-jquery-tests.ts b/jasmine-jquery/jasmine-jquery-tests.ts new file mode 100644 index 0000000000..15877ed60d --- /dev/null +++ b/jasmine-jquery/jasmine-jquery-tests.ts @@ -0,0 +1,135 @@ +/// +/// +/// + +describe("Jasmine jQuery extension", () => { + it("Adds jQuery matchers", () => { + expect($('
')).toBe('div'); + expect($('
')).toBe('div#some-id'); + expect($('')).toBeChecked(); + expect($('
')).toBeHidden(); + expect($('
')).toHaveCss({ display: "none", margin: "10px" }); + expect($('
')).toHaveCss({ margin: "10px" }); + expect($('')).toBeSelected(); + expect($('
')).toBeVisible(); + expect($('
')).toContain('span.some-class'); + expect($('').addClass('js-something')).toBeMatchedBy('.js-something'); + expect($('')).toExist(); + expect($('
')).toHaveAttr('id', 'some-id'); + expect($('
')).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($('
')).toHaveClass("some-class"); + expect($('
')).toHaveData('item', 'value'); + expect($('
')).toHaveHtml(''); + expect($('

    header

    ')).toContainHtml('
      '); + expect($('

        header

        ')).toContainText('header'); + expect($('
        ')).toHaveId("some-id"); + expect($('
        some text
        ')).toHaveText('some text'); + expect($('')).toHaveValue('some text'); + expect($('ul > li')).toHaveLength(3); + expect($('')).toBeDisabled(); + expect($('').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(''); + jasmine.getFixtures().appendSet(''); + jasmine.getFixtures().preload('myfixture.html', 'myfixture2.html'); + jasmine.getFixtures().clearCache(); + jasmine.getFixtures().cleanUp(); + + loadFixtures('myfixture.html'); + appendLoadFixtures('myfixture.html'); + readFixtures('myfixture.html'); + setFixtures(''); + appendSetFixtures(''); + + 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(); + }); + }); +}) diff --git a/jasmine-jquery/jasmine-jquery.d.ts b/jasmine-jquery/jasmine-jquery.d.ts index 0f281d637a..f6c117d49f 100644 --- a/jasmine-jquery/jasmine-jquery.d.ts +++ b/jasmine-jquery/jasmine-jquery.d.ts @@ -5,22 +5,26 @@ /// +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;