UNPKG

cybernaut

Version:

Reliable, automated web UI testing in BDD-style.

129 lines 6.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const execution_1 = require("../execution"); const test_utils_1 = require("./test-utils"); const driver = {}; test_utils_1.given('a newly created executor() is called', () => { let action; let executor; beforeEach(() => { action = { description: '<actionDescription>', perform: jest.fn() }; executor = execution_1.createExecutor(action); }); test_utils_1.then('it should call action.perform() once', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield executor(driver, 2, 1); expect(action.perform.mock.calls.length).toBe(1); expect(action.perform.mock.calls[0][0]).toBe(driver); })); test_utils_1.when('the call to action.perform() does not throw an error', () => { test_utils_1.then('it should return a successful execution', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const execution = yield executor(driver, 1, 0); expect(execution.description).toBe('<actionDescription>'); expect(execution.error).toBe(false); expect((yield executor(driver, 1, 1)).description).toBe('<actionDescription>'); expect((yield executor(driver, 1, 2)).description).toBe('<actionDescription>'); expect((yield executor(driver, 2, 1)).description).toBe('<actionDescription>' + ' (attempt 2 of 2)'); expect((yield executor(driver, 2, 2)).description).toBe('<actionDescription>' + ' (attempt 2 of 3)'); })); }); test_utils_1.when('the call to action.perform() throws an error', () => { test_utils_1.then('it should return an erroneous execution', () => tslib_1.__awaiter(this, void 0, void 0, function* () { action.perform.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw new Error('<cause>'); })); action.perform.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw new Error(); })); action.perform.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw undefined; })); for (const message of ['<cause>', 'unknown error', 'unknown error']) { const execution = yield executor(driver, 2, 1); expect(execution.description).toBe(`<actionDescription> (${message})`); expect(execution.error).toBe(true); } })); }); }); test_utils_1.given('execute() is called with retries=1', () => { const options = { retries: 1, retryDelay: 0 }; let executor; beforeEach(() => { executor = jest.fn(); }); test_utils_1.when('any call to executor() returns a successful execution', () => { beforeEach(() => { executor.mockImplementation(() => tslib_1.__awaiter(this, void 0, void 0, function* () { return ({ description: 'attempt 1', error: false }); })); }); test_utils_1.then('it should call executor() once', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield execution_1.execute(executor, driver, options); expect(executor.mock.calls.length).toBe(1); expect(executor.mock.calls[0][0]).toBe(driver); expect(executor.mock.calls[0][1]).toBe(1); expect(executor.mock.calls[0][2]).toBe(options.retries); })); test_utils_1.then('it should return the execution', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const execution = yield execution_1.execute(executor, driver, options); expect(execution).toEqual({ description: 'attempt 1', error: false }); })); }); test_utils_1.when('any call to executor() returns an erroneous execution', () => { beforeEach(() => { executor.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { return ({ description: 'attempt 1', error: true }); })); executor.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { return ({ description: 'attempt 2', error: true }); })); }); test_utils_1.then('it should call executor() twice', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield execution_1.execute(executor, driver, options); expect(executor.mock.calls.length).toBe(2); expect(executor.mock.calls[0][0]).toBe(driver); expect(executor.mock.calls[0][1]).toBe(1); expect(executor.mock.calls[0][2]).toBe(options.retries); expect(executor.mock.calls[1][0]).toBe(driver); expect(executor.mock.calls[1][1]).toBe(2); expect(executor.mock.calls[1][2]).toBe(options.retries); })); test_utils_1.then('it should return the second execution', () => tslib_1.__awaiter(this, void 0, void 0, function* () { const execution = yield execution_1.execute(executor, driver, options); expect(execution).toEqual({ description: 'attempt 2', error: true }); })); test_utils_1.then('it should delay the second call to executor()', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { jest.useFakeTimers(); const retryDelay = 123; const promise = execution_1.execute(executor, driver, Object.assign({}, options, { retryDelay })); yield test_utils_1.shortSleep(); expect(executor.mock.calls.length).toBe(1); jest.runTimersToTime(retryDelay - 1); yield test_utils_1.shortSleep(); expect(executor.mock.calls.length).toBe(1); jest.runTimersToTime(1); yield test_utils_1.shortSleep(); expect(executor.mock.calls.length).toBe(2); yield promise; } finally { jest.useRealTimers(); } })); }); }); //# sourceMappingURL=execution.test.js.map