UNPKG

cybernaut

Version:

Reliable, automated web UI testing in BDD-style.

355 lines 18.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const test_context_1 = require("../test-context"); const test_utils_1 = require("./test-utils"); const defaultOptions = { retries: 0, retryDelay: 10 }; const error = new Error('<cause>'); test_utils_1.given('a new test context is created with retries=0 and retryDelay=10', () => { let accessor; let action; let logger; let predicate; let t; beforeEach(() => { accessor = { description: '<accessorDescription>', get: jest.fn() }; action = { description: '<actionDescription>', perform: jest.fn() }; logger = { pass: jest.fn() }; predicate = { compare: jest.fn().mockReturnValue('<predicateComparison>'), description: '<predicateDescription>', test: jest.fn() }; t = new test_context_1.TestContext({}, logger, defaultOptions); }); test_utils_1.when('t.assert() is called without options', () => { test_utils_1.and('the verification is valid in the first attempt', () => { beforeEach(() => { predicate.test.mockReturnValue(true); }); test_utils_1.then('it should call logger.pass() without attempts', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield t.assert(accessor, predicate); expect(logger.pass.mock.calls.length).toBe(1); expect(logger.pass.mock.calls[0][0]).toBe('Assert: <accessorDescription> <predicateDescription>'); })); }); test_utils_1.and('the verification is invalid until the second attempt', () => { beforeEach(() => { predicate.test.mockReturnValueOnce(false); predicate.test.mockReturnValue(true); }); test_utils_1.then('it should throw an error with a comparison', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield expect(t.assert(accessor, predicate)).rejects.toEqual(new Error('Assert: <accessorDescription> <predicateDescription> ' + '(<predicateComparison>)')); })); test_utils_1.then('it should not call logger.pass()', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield t.assert(accessor, predicate); } catch (e) { /* */ } expect(logger.pass.mock.calls.length).toBe(0); })); }); test_utils_1.and('the verification is erroneous in the first attempt', () => { beforeEach(() => { accessor.get.mockImplementation(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw error; })); }); test_utils_1.then('it should throw an error with a cause', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield expect(t.assert(accessor, predicate)).rejects.toEqual(new Error('Assert: <accessorDescription> <predicateDescription> (<cause>)')); })); test_utils_1.then('it should not call logger.pass()', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield t.assert(accessor, predicate); } catch (e) { /* */ } expect(logger.pass.mock.calls.length).toBe(0); })); }); }); test_utils_1.when('t.assert() is called with retries=1', () => { const options = { retries: 1 }; test_utils_1.and('the verification is invalid until the second attempt', () => { beforeEach(() => { predicate.test.mockReturnValueOnce(false); predicate.test.mockReturnValue(true); }); test_utils_1.then('it should call logger.pass() with attempts', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield t.assert(accessor, predicate, options); expect(logger.pass.mock.calls.length).toBe(1); expect(logger.pass.mock.calls[0][0]).toBe('Assert: <accessorDescription> <predicateDescription> ' + '(attempt 2 of 2)'); })); test_utils_1.then('it should delay the second attempt by 10 ms', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { jest.useFakeTimers(); const promise = t.assert(accessor, predicate, options); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(defaultOptions.retryDelay - 1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(2); expect(predicate.test.mock.calls.length).toBe(2); yield promise; } finally { jest.useRealTimers(); } })); }); }); test_utils_1.when('t.assert() is called with retries=1 and retryDelay=20', () => { const options = { retries: 1, retryDelay: 20 }; test_utils_1.and('the verification is invalid until the second attempt', () => { beforeEach(() => { predicate.test.mockReturnValueOnce(false); predicate.test.mockReturnValue(true); }); test_utils_1.then('it should delay the second attempt by 20 ms', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { jest.useFakeTimers(); const promise = t.assert(accessor, predicate, options); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(options.retryDelay - 1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(2); expect(predicate.test.mock.calls.length).toBe(2); yield promise; } finally { jest.useRealTimers(); } })); }); }); test_utils_1.when('t.perform() is called without options', () => { test_utils_1.and('the execution is successful in the first attempt', () => { test_utils_1.then('it should call logger.pass() without attempts', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield t.perform(action); expect(logger.pass.mock.calls.length).toBe(1); expect(logger.pass.mock.calls[0][0]).toBe('Perform: <actionDescription>'); })); }); test_utils_1.and('the execution is erroneous until the second attempt', () => { beforeEach(() => { action.perform.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw error; })); }); test_utils_1.then('it should throw an error with a cause', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield expect(t.perform(action)).rejects.toEqual(new Error('Perform: <actionDescription> (<cause>)')); })); test_utils_1.then('it should not call logger.pass()', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield t.perform(action); } catch (e) { /* */ } expect(logger.pass.mock.calls.length).toBe(0); })); }); }); test_utils_1.when('t.perform() is called with retries=1', () => { const options = { retries: 1 }; test_utils_1.and('the execution is erroneous until the second attempt', () => { beforeEach(() => { action.perform.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw error; })); }); test_utils_1.then('it should call logger.pass() with attempts', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield t.perform(action, options); expect(logger.pass.mock.calls.length).toBe(1); expect(logger.pass.mock.calls[0][0]).toBe('Perform: <actionDescription> (attempt 2 of 2)'); })); test_utils_1.then('it should delay the second attempt by 10 ms', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { jest.useFakeTimers(); const promise = t.perform(action, options); yield test_utils_1.shortSleep(); expect(action.perform.mock.calls.length).toBe(1); jest.runTimersToTime(defaultOptions.retryDelay - 1); yield test_utils_1.shortSleep(); expect(action.perform.mock.calls.length).toBe(1); jest.runTimersToTime(1); yield test_utils_1.shortSleep(); expect(action.perform.mock.calls.length).toBe(2); yield promise; } finally { jest.useRealTimers(); } })); }); }); test_utils_1.when('t.perform() is called with retries=1 and retryDelay=20', () => { const options = { retries: 1, retryDelay: 20 }; test_utils_1.and('the execution is erroneous until the second attempt', () => { beforeEach(() => { action.perform.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw error; })); }); test_utils_1.then('it should delay the second attempt by 20 ms', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { jest.useFakeTimers(); const promise = t.perform(action, options); yield test_utils_1.shortSleep(); expect(action.perform.mock.calls.length).toBe(1); jest.runTimersToTime(options.retryDelay - 1); yield test_utils_1.shortSleep(); expect(action.perform.mock.calls.length).toBe(1); jest.runTimersToTime(1); yield test_utils_1.shortSleep(); expect(action.perform.mock.calls.length).toBe(2); yield promise; } finally { jest.useRealTimers(); } })); }); }); test_utils_1.when('t.verify() is called without options', () => { test_utils_1.and('the verification is valid in the first attempt', () => { beforeEach(() => { predicate.test.mockReturnValue(true); }); test_utils_1.then('it should return true', () => tslib_1.__awaiter(this, void 0, void 0, function* () { expect(yield t.verify(accessor, predicate)).toBe(true); })); test_utils_1.then('it should call logger.pass() without attempts', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield t.verify(accessor, predicate); expect(logger.pass.mock.calls.length).toBe(1); expect(logger.pass.mock.calls[0][0]).toBe('Verify: <accessorDescription> <predicateDescription>'); })); }); test_utils_1.and('the verification is invalid until the second attempt', () => { beforeEach(() => { predicate.test.mockReturnValueOnce(false); predicate.test.mockReturnValue(true); }); test_utils_1.then('it should return false', () => tslib_1.__awaiter(this, void 0, void 0, function* () { expect(yield t.verify(accessor, predicate)).toBe(false); })); test_utils_1.then('it should call logger.pass() with a comparison', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield t.verify(accessor, predicate); expect(logger.pass.mock.calls.length).toBe(1); expect(logger.pass.mock.calls[0][0]).toBe('Verify: <accessorDescription> <predicateDescription> ' + '(<predicateComparison>)'); })); }); test_utils_1.and('the verification is erroneous in the first attempt', () => { beforeEach(() => { accessor.get.mockImplementation(() => tslib_1.__awaiter(this, void 0, void 0, function* () { throw error; })); }); test_utils_1.then('it should throw an error with a cause', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield expect(t.verify(accessor, predicate)).rejects.toEqual(new Error('Verify: <accessorDescription> <predicateDescription> (<cause>)')); })); test_utils_1.then('it should not call logger.pass()', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { yield t.verify(accessor, predicate); } catch (e) { /* */ } expect(logger.pass.mock.calls.length).toBe(0); })); }); }); test_utils_1.when('t.verify() is called with retries=1', () => { const options = { retries: 1 }; test_utils_1.and('the verification is invalid until the second attempt', () => { beforeEach(() => { predicate.test.mockReturnValueOnce(false); predicate.test.mockReturnValue(true); }); test_utils_1.then('it should return true', () => tslib_1.__awaiter(this, void 0, void 0, function* () { expect(yield t.verify(accessor, predicate, options)).toBe(true); })); test_utils_1.then('it should call logger.pass() with attempts', () => tslib_1.__awaiter(this, void 0, void 0, function* () { yield t.verify(accessor, predicate, options); expect(logger.pass.mock.calls.length).toBe(1); expect(logger.pass.mock.calls[0][0]).toBe('Verify: <accessorDescription> <predicateDescription> ' + '(attempt 2 of 2)'); })); test_utils_1.then('it should delay the second attempt by 10 ms', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { jest.useFakeTimers(); const promise = t.verify(accessor, predicate, options); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(defaultOptions.retryDelay - 1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(2); expect(predicate.test.mock.calls.length).toBe(2); yield promise; } finally { jest.useRealTimers(); } })); }); }); test_utils_1.when('t.verify() is called with retries=1 and retryDelay=20', () => { const options = { retries: 1, retryDelay: 20 }; test_utils_1.and('the verification is invalid until the second attempt', () => { beforeEach(() => { predicate.test.mockReturnValueOnce(false); predicate.test.mockReturnValue(true); }); test_utils_1.then('it should delay the second attempt by 20 ms', () => tslib_1.__awaiter(this, void 0, void 0, function* () { try { jest.useFakeTimers(); const promise = t.verify(accessor, predicate, options); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(options.retryDelay - 1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(1); expect(predicate.test.mock.calls.length).toBe(1); jest.runTimersToTime(1); yield test_utils_1.shortSleep(); expect(accessor.get.mock.calls.length).toBe(2); expect(predicate.test.mock.calls.length).toBe(2); yield promise; } finally { jest.useRealTimers(); } })); }); }); }); //# sourceMappingURL=test-context.test.js.map