Skip to content

Commit

Permalink
feat: custom matcher toHaveText
Browse files Browse the repository at this point in the history
  • Loading branch information
cexbrayat authored and jnizet committed May 26, 2018
1 parent 065ab58 commit 8036c22
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions projects/ngx-speculoos/src/jasmine-matchers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ declare namespace jasmine {
*/
toHaveValue(value: string): boolean;

/**
* Checks that the receiver is a TestElement wrapping a DOM element and has the exact given textContent
*/
toHaveText(textContent: string): boolean;

/**
* Checks that the receiver is a TestElement wrapping a DOM element and contains the given textContent
*/
Expand Down
57 changes: 57 additions & 0 deletions projects/ngx-speculoos/src/lib/matchers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,63 @@ describe('Custom matchers', () => {

});

describe('toHaveText', () => {
const matcher = speculoosMatchers.toHaveText(undefined, undefined);

it('should check for a textContent', () => {
expect(tester.div).toHaveText('Hello');
expect(tester.div).not.toHaveText('He');
expect(tester.div).not.toHaveText('baz');
});

it('should return false if wrong textContent', () => {
const result = matcher.compare(tester.div, 'baz');
expect(result.pass).toBeFalsy();
expect(result.message).toBe(`Expected element to have textContent 'baz', but had textContent 'Hello'`);
});

it('should return true if wrong textContent and .not', () => {
const result = matcher.negativeCompare(tester.div, 'baz');
expect(result.pass).toBeTruthy();
});

it('should return false if no textContent', () => {
const result = matcher.compare(tester.name, 'baz');
expect(result.pass).toBeFalsy();
expect(result.message).toBe(`Expected element to have textContent 'baz', but had no textContent`);
});

it('should return true if no textContent and .not', () => {
const result = matcher.negativeCompare(tester.name, 'baz');
expect(result.pass).toBeTruthy();
});

it('should return false if no element', () => {
const result = matcher.compare(null, 'baz');
expect(result.pass).toBeFalsy();
expect(result.message).toBe(`Expected to check textContent 'baz' on element, but element was falsy`);
});

it('should return false if no element and .not too', () => {
const result = matcher.negativeCompare(null, 'baz');
expect(result.pass).toBeFalsy();
expect(result.message).toBe(`Expected to check textContent 'baz' on element, but element was falsy`);
});

it('should return false if element of wrong type', () => {
const result = matcher.compare('hello', 'baz');
expect(result.pass).toBeFalsy();
expect(result.message).toBe(`Expected to check textContent 'baz' on element, but element was not a TestElement`);
});

it('should return false if element of wrong type and .not too', () => {
const result = matcher.negativeCompare('hello', 'baz');
expect(result.pass).toBeFalsy();
expect(result.message).toBe(`Expected to check textContent 'baz' on element, but element was not a TestElement`);
});

});

describe('toContainText', () => {
const matcher = speculoosMatchers.toContainText(undefined, undefined);

Expand Down
32 changes: 32 additions & 0 deletions projects/ngx-speculoos/src/lib/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ const speculoosMatchers: jasmine.CustomMatcherFactories = {
};
},

/**
* Checks that the receiver is a TestElement wrapping a DOM element and has the exact given textContent
*/
toHaveText: (util: jasmine.MatchersUtil, customEqualityTesters: Array<jasmine.CustomEqualityTester>): jasmine.CustomMatcher => {
const assert = (isNegative: boolean, el: any, expected: string) => {
if (!el) {
return { pass: false, message: `Expected to check textContent '${expected}' on element, but element was falsy` };
}
if (!(el instanceof TestElement)) {
return { pass: false, message: `Expected to check textContent '${expected}' on element, but element was not a TestElement` };
}
const actual = el.textContent;
if (!actual) {
return {
pass: isNegative,
message: `Expected element to ${isNegative ? 'not ' : ''}have textContent '${expected}', but had no textContent`
};
}
const pass = actual === expected;
const message = `Expected element to ${isNegative ? 'not ' : ''}have textContent '${expected}', but had textContent '${actual}'`;
return { pass: isNegative ? !pass : pass, message };
};
return {
compare: (el: any, expected: string): jasmine.CustomMatcherResult => {
return assert(false, el, expected);
},
negativeCompare: (el: any, expected: string): jasmine.CustomMatcherResult => {
return assert(true, el, expected);
}
};
},

/**
* Checks that the receiver is a TestElement wrapping a DOM element and contains the given textContent
*/
Expand Down

0 comments on commit 8036c22

Please sign in to comment.