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(); // NOTE: It is now necessary to explicitly add the generic parameter when using `toContain` expect($('
')).toContain('span.some-class'); expect($('').addClass('js-something')).toBeMatchedBy('.js-something'); expect($('')).toExist(); expect($('
')).toHaveAttr('id', 'some-id'); expect($('')).toHaveAttr('type'); expect($('
')).toHaveProp('id', 'some-id'); expect($('')).toHaveProp('checked'); 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) expect($('
')).toBeInDOM(); expect($('
')).toContainElement('span.some-class'); expect($('

header

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

header

')).toContainText('header') }); 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'); }); 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(); }); }); });