Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Component } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ComponentTester } from './component-tester'; | ||
import { elementMatchers } from './matchers'; | ||
|
||
@Component({ | ||
template: ` | ||
<div id="classes" class="foo bar">Hello</div> | ||
<div id="none">Hello</div> | ||
` | ||
}) | ||
class TestComponent { | ||
} | ||
|
||
class TestComponentTester extends ComponentTester<TestComponent> { | ||
constructor() { | ||
super(TestComponent); | ||
} | ||
|
||
get div() { | ||
return this.element('#classes'); | ||
} | ||
|
||
get none() { | ||
return this.element('#none'); | ||
} | ||
} | ||
|
||
describe('Custom matchers', () => { | ||
|
||
let tester: TestComponentTester; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ declarations: [TestComponent] }); | ||
tester = new TestComponentTester(); | ||
tester.detectChanges(); | ||
jasmine.addMatchers(elementMatchers); | ||
}); | ||
|
||
it('should check for a class', () => { | ||
expect(tester.div).toHaveClass('foo'); | ||
expect(tester.div).not.toHaveClass('baz'); | ||
|
||
const matcher = elementMatchers.toHaveClass(undefined, undefined); | ||
|
||
// missing class | ||
let result = matcher.compare(tester.div, 'baz'); | ||
expect(result.pass).toBeFalsy(); | ||
expect(result.message).toBe(`Expected element to have class 'baz', but had 'foo, bar'`); | ||
|
||
// no class | ||
result = matcher.compare(tester.none, 'baz'); | ||
expect(result.pass).toBeFalsy(); | ||
expect(result.message).toBe(`Expected element to have class 'baz', but had none`); | ||
|
||
// null element | ||
result = matcher.compare(null, 'baz'); | ||
expect(result.pass).toBeFalsy(); | ||
expect(result.message).toBe(`Expected element to have class 'baz', but element was falsy`); | ||
|
||
// not a TestElement | ||
result = matcher.compare('hello', 'baz'); | ||
expect(result.pass).toBeFalsy(); | ||
expect(result.message).toBe(`Expected element to have class 'baz', but element was not a TestElement`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { TestElement } from './test-element'; | ||
const elementMatchers: jasmine.CustomMatcherFactories = { | ||
|
||
/** | ||
* Checks that an element has the specified class | ||
*/ | ||
toHaveClass: (util: jasmine.MatchersUtil, customEqualityTesters: Array<jasmine.CustomEqualityTester>): jasmine.CustomMatcher => { | ||
return { | ||
compare: (el: any, expected: string): jasmine.CustomMatcherResult => { | ||
if (!el) { | ||
return { pass: false, message: `Expected element to have class '${expected}', but element was falsy` }; | ||
} | ||
if (!(el instanceof TestElement)) { | ||
return { pass: false, message: `Expected element to have class '${expected}', but element was not a TestElement` }; | ||
} | ||
const actual = el.classes; | ||
const pass = actual.includes(expected); | ||
const message = `Expected element to have class '${expected}', but had ${actual.length ? '\'' + actual.join(', ') + '\'' : 'none'}`; | ||
return { pass, message }; | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
export { elementMatchers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare namespace jasmine { | ||
interface Matchers<T> { | ||
toHaveClass(className: string): boolean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters