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

Commit

Permalink
feat: add when method (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
clebert committed Feb 28, 2018
1 parent 5b2f304 commit 6d5a3d8
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 3 deletions.
128 changes: 127 additions & 1 deletion @pageobject/reliable/src/TestCase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,123 @@ describe('TestCase', () => {
});
});

describe('when()', () => {
it('should only run the then-test-case', async () => {
const condition = new ConditionMock();

condition.test.mockImplementation(async => true);

const thenAction = new ActionMock();
const otherwiseAction = new ActionMock();

await testCase
.when(condition as any, (then, otherwise) => {
expect(then.defaultTimeout).toBe(testCase.defaultTimeout);

then.perform(thenAction);
otherwise.perform(otherwiseAction);
})
.run();

expect(condition.test).toHaveBeenCalledTimes(1);
expect(thenAction.perform).toHaveBeenCalledTimes(1);
expect(otherwiseAction.perform).toHaveBeenCalledTimes(0);
});

it('should only run the otherwise-test-case', async () => {
const condition = new ConditionMock();

condition.test.mockImplementation(async => false);

const thenAction = new ActionMock();
const otherwiseAction = new ActionMock();

await testCase
.when(condition as any, (then, otherwise) => {
expect(otherwise.defaultTimeout).toBe(testCase.defaultTimeout);

then.perform(thenAction);
otherwise.perform(otherwiseAction);
})
.run();

expect(condition.test).toHaveBeenCalledTimes(1);
expect(thenAction.perform).toHaveBeenCalledTimes(0);
expect(otherwiseAction.perform).toHaveBeenCalledTimes(1);
});

it('should throw a condition-test error', async () => {
const condition = new ConditionMock();

condition.test.mockImplementation(erroneous());

const thenAction = new ActionMock();
const otherwiseAction = new ActionMock();

await expect(
testCase
.when(condition as any, (then, otherwise) => {
then.perform(thenAction);
otherwise.perform(otherwiseAction);
})
.run()
).rejects.toEqual(new Error('When: description\n Cause: Error'));

expect(condition.test).toHaveBeenCalledTimes(1);
expect(thenAction.perform).toHaveBeenCalledTimes(0);
expect(otherwiseAction.perform).toHaveBeenCalledTimes(0);
});

it('should throw a then-test-case error', async () => {
const condition = new ConditionMock();

condition.test.mockImplementation(async => true);

const thenAction = new ActionMock();

thenAction.perform.mockImplementationOnce(erroneous());

const otherwiseAction = new ActionMock();

await expect(
testCase
.when(condition as any, (then, otherwise) => {
then.perform(thenAction);
otherwise.perform(otherwiseAction);
})
.run()
).rejects.toEqual(new Error('Perfom: description\n Cause: Error'));

expect(condition.test).toHaveBeenCalledTimes(1);
expect(thenAction.perform).toHaveBeenCalledTimes(1);
expect(otherwiseAction.perform).toHaveBeenCalledTimes(0);
});

it('should throw an otherwise-test-case error', async () => {
const condition = new ConditionMock();

condition.test.mockImplementation(async => false);

const thenAction = new ActionMock();
const otherwiseAction = new ActionMock();

otherwiseAction.perform.mockImplementationOnce(erroneous());

await expect(
testCase
.when(condition as any, (then, otherwise) => {
then.perform(thenAction);
otherwise.perform(otherwiseAction);
})
.run()
).rejects.toEqual(new Error('Perfom: description\n Cause: Error'));

expect(condition.test).toHaveBeenCalledTimes(1);
expect(thenAction.perform).toHaveBeenCalledTimes(0);
expect(otherwiseAction.perform).toHaveBeenCalledTimes(1);
});
});

describe('run()', async () => {
it('should run the test steps sequentially', async () => {
const calls: string[] = [];
Expand All @@ -207,12 +324,21 @@ describe('TestCase', () => {
calls.push('perform()');
});

condition.test.mockImplementation(async () => true);

const conditionalAction = new ActionMock();

conditionalAction.perform.mockImplementation(async () => {
calls.push('when()');
});

await testCase
.assert(condition as any)
.when(condition as any, then => then.perform(conditionalAction))
.perform(action)
.run();

expect(calls).toEqual(['assert()', 'perform()']);
expect(calls).toEqual(['assert()', 'when()', 'perform()']);
});

it('should throw an already-run error', async () => {
Expand Down
28 changes: 28 additions & 0 deletions @pageobject/reliable/src/TestCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ export class TestCase {
return this;
}

public when(
condition: Condition<any> /* tslint:disable-line no-any */,
callback: (then: TestCase, otherwise: TestCase) => void
): this {
this._testSteps.push(async () => {
const thenTestCase = new TestCase(this.defaultTimeout);
const otherwiseTestCase = new TestCase(this.defaultTimeout);

callback(thenTestCase, otherwiseTestCase);

let result: boolean;

try {
result = await condition.test();
} catch (e) {
throw createError('When', condition.describe(), e.message);
}

if (result) {
await thenTestCase.run();
} else {
await otherwiseTestCase.run();
}
});

return this;
}

public async run(): Promise<void> {
if (this._alreadyRun) {
throw new Error(
Expand Down

0 comments on commit 6d5a3d8

Please sign in to comment.