Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
feat(base): add includes and not-includes predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
clebert committed Apr 19, 2018
1 parent 68271c3 commit e88338e
Show file tree
Hide file tree
Showing 21 changed files with 3,902 additions and 80 deletions.
102 changes: 102 additions & 0 deletions @pageobject/base/src/Predicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const {
isGreaterThanOrEquals,
isLessThan,
isLessThanOrEquals,
includes,
notIncludes,
matches,
notMatches
} = Predicate;
Expand Down Expand Up @@ -85,6 +87,22 @@ const effectsIsLessThanOrEquals = (key: keyof Predicate<number>) => {
};
};

const effectsIncludes = (
key: keyof Predicate<string>,
negated: boolean = false
) => {
const factory = negated ? notIncludes : includes;

return {
ok: [
() => factory('foo')[key]('foo'),
() => factory('foo')[key]('foobar'),
() => factory('foo')[key]('barfoo')
],
nok: [() => factory('foo')[key]('bar')]
};
};

const effectsMatches = (
key: keyof Predicate<string>,
negated: boolean = false
Expand Down Expand Up @@ -354,6 +372,90 @@ describe('Predicate', () => {
});
});

describe('includes() => Predicate', () => {
describe('assert()', () => {
it('should not throw a jest error', () => {
for (const effect of effectsIncludes('assert').ok) {
expect(effect).not.toThrow();
}
});

it('should throw a jest error', () => {
for (const effect of effectsIncludes('assert').nok) {
expect(effect).toThrow('To contain value:');
}
});

it('should not throw a node error', () => {
for (const effect of effectsIncludes('assert').ok) {
expect(noJest(effect)).not.toThrow();
}
});

it('should throw a node error', () => {
for (const effect of effectsIncludes('assert').nok) {
expect(noJest(effect)).toThrow(/'[a-z]+' =~ 'foo'/);
}
});
});

describe('test()', () => {
it('should return true', () => {
for (const effect of effectsIncludes('test').ok) {
expect(effect()).toBe(true);
}
});

it('should return false', () => {
for (const effect of effectsIncludes('test').nok) {
expect(effect()).toBe(false);
}
});
});
});

describe('notIncludes() => Predicate', () => {
describe('assert()', () => {
it('should not throw a jest error', () => {
for (const effect of effectsIncludes('assert', true).nok) {
expect(effect).not.toThrow();
}
});

it('should throw a jest error', () => {
for (const effect of effectsIncludes('assert', true).ok) {
expect(effect).toThrow('Not to contain value:');
}
});

it('should not throw a node error', () => {
for (const effect of effectsIncludes('assert', true).nok) {
expect(noJest(effect)).not.toThrow();
}
});

it('should throw a node error', () => {
for (const effect of effectsIncludes('assert', true).ok) {
expect(noJest(effect)).toThrow(/'[a-z]+' !~ 'foo'/);
}
});
});

describe('test()', () => {
it('should return true', () => {
for (const effect of effectsIncludes('test', true).nok) {
expect(effect()).toBe(true);
}
});

it('should return false', () => {
for (const effect of effectsIncludes('test', true).ok) {
expect(effect()).toBe(false);
}
});
});
});

describe('matches() => Predicate', () => {
describe('assert()', () => {
it('should not throw a jest error', () => {
Expand Down
64 changes: 54 additions & 10 deletions @pageobject/base/src/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export abstract class Predicate<TValue> {
return new IsLessThanOrEquals(expected);
}

public static includes(expected: string): Predicate<string> {
return new Includes(expected);
}

public static notIncludes(expected: string): Predicate<string> {
return new NotIncludes(expected);
}

public static matches(expected: RegExp): Predicate<string> {
return new Matches(expected);
}
Expand Down Expand Up @@ -76,7 +84,7 @@ class Is<TValue> extends BinaryPredicate<TValue> {
}
}

class IsNot<TValue> extends BinaryPredicate<TValue> {
class IsNot<TValue> extends Is<TValue> {
public assert(actual: TValue): void {
if (useJest()) {
expect(actual).not.toBe(this.expected);
Expand All @@ -86,7 +94,7 @@ class IsNot<TValue> extends BinaryPredicate<TValue> {
}

public test(actual: TValue): boolean {
return actual !== this.expected;
return !super.test(actual);
}
}

Expand Down Expand Up @@ -146,14 +154,49 @@ class IsLessThanOrEquals extends BinaryPredicate<number> {
}
}

class Matches extends BinaryPredicate<string, RegExp> {
class Includes extends BinaryPredicate<string> {
public assert(actual: string): void {
if (useJest()) {
expect(actual).toContain(this.expected);
} else {
ok(
this.test(actual),
`${serialize(actual)} =~ ${serialize(this.expected)}`
);
}
}

public test(actual: string): boolean {
return actual.includes(this.expected);
}
}

class NotIncludes extends Includes {
public assert(actual: string): void {
const message = `${serialize(actual)} =~ ${serialize(this.expected)}`;
if (useJest()) {
expect(actual).not.toContain(this.expected);
} else {
ok(
this.test(actual),
`${serialize(actual)} !~ ${serialize(this.expected)}`
);
}
}

public test(actual: string): boolean {
return !super.test(actual);
}
}

class Matches extends BinaryPredicate<string, RegExp> {
public assert(actual: string): void {
if (useJest()) {
expect(actual).toMatch(this.expected);
} else {
ok(this.test(actual), message);
ok(
this.test(actual),
`${serialize(actual)} =~ ${serialize(this.expected)}`
);
}
}

Expand All @@ -162,18 +205,19 @@ class Matches extends BinaryPredicate<string, RegExp> {
}
}

class NotMatches extends BinaryPredicate<string, RegExp> {
class NotMatches extends Matches {
public assert(actual: string): void {
const message = `${serialize(actual)} !~ ${serialize(this.expected)}`;

if (useJest()) {
expect(actual).not.toMatch(this.expected);
} else {
ok(this.test(actual), message);
ok(
this.test(actual),
`${serialize(actual)} !~ ${serialize(this.expected)}`
);
}
}

public test(actual: string): boolean {
return !this.expected.test(actual);
return !super.test(actual);
}
}
2 changes: 1 addition & 1 deletion docs/api/base/assets/js/search.js

Large diffs are not rendered by default.

0 comments on commit e88338e

Please sign in to comment.