cybernaut
Version:
Reliable, automated web UI testing in BDD-style.
184 lines • 9.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const verification_1 = require("../verification");
const test_utils_1 = require("./test-utils");
const driver = {};
test_utils_1.given('a newly created verifier() is called', () => {
let accessor;
let description;
let predicate;
let verifier;
beforeEach(() => {
accessor = {
description: '<accessorDescription>',
get: jest.fn()
};
predicate = {
compare: jest.fn().mockReturnValue('<predicateComparison>'),
description: '<predicateDescription>',
test: jest.fn()
};
description = '<accessorDescription> <predicateDescription>';
verifier = verification_1.createVerifier(accessor, predicate);
});
test_utils_1.then('it should call accessor.get() once', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
yield verifier(driver, 2, 1);
expect(accessor.get.mock.calls.length).toBe(1);
expect(accessor.get.mock.calls[0][0]).toBe(driver);
}));
test_utils_1.when('the call to accessor.get() returns an actual value', () => {
beforeEach(() => {
accessor.get.mockImplementation(() => tslib_1.__awaiter(this, void 0, void 0, function* () { return '<actualValue>'; }));
});
test_utils_1.then('it should call predicate.test() once', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
yield verifier(driver, 2, 1);
expect(predicate.test.mock.calls.length).toBe(1);
expect(predicate.test.mock.calls[0][0]).toBe('<actualValue>');
}));
test_utils_1.when('the call to predicate.test() returns true', () => {
test_utils_1.then('it should return a valid verification', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
predicate.test.mockReturnValue(true);
const verification = yield verifier(driver, 1, 0);
expect(verification.description).toBe(description);
expect(verification.result).toBe('valid');
expect((yield verifier(driver, 1, 1)).description).toBe(description);
expect((yield verifier(driver, 1, 2)).description).toBe(description);
expect((yield verifier(driver, 2, 1)).description).toBe(description + ' (attempt 2 of 2)');
expect((yield verifier(driver, 2, 2)).description).toBe(description + ' (attempt 2 of 3)');
}));
});
test_utils_1.when('the call to predicate.test() returns false', () => {
beforeEach(() => {
predicate.test.mockReturnValue(false);
});
test_utils_1.then('it should call predicate.compare() once', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
yield verifier(driver, 2, 1);
expect(predicate.compare.mock.calls.length).toBe(1);
expect(predicate.compare.mock.calls[0][0]).toBe('<actualValue>');
}));
test_utils_1.then('it should return an invalid verification', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
const verification = yield verifier(driver, 2, 1);
expect(verification.description).toBe(`${description} (<predicateComparison>)`);
expect(verification.result).toBe('invalid');
}));
});
test_utils_1.when('the call to predicate.test() throws an error', () => {
test_utils_1.then('it should return an erroneous verification', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
predicate.test.mockImplementationOnce(() => {
throw new Error('<cause>');
});
predicate.test.mockImplementationOnce(() => {
throw new Error();
});
predicate.test.mockImplementationOnce(() => {
throw undefined;
});
for (const message of ['<cause>', 'unknown error', 'unknown error']) {
const verification = yield verifier(driver, 2, 1);
expect(verification.description).toBe(`${description} (${message})`);
expect(verification.result).toBe('error');
}
}));
});
});
test_utils_1.when('the call to accessor.get() throws an error', () => {
test_utils_1.then('it should return an erroneous verification', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
accessor.get.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
throw new Error('<cause>');
}));
accessor.get.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
throw new Error();
}));
accessor.get.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
throw undefined;
}));
for (const message of ['<cause>', 'unknown error', 'unknown error']) {
const verification = yield verifier(driver, 2, 1);
expect(verification.description).toBe(`${description} (${message})`);
expect(verification.result).toBe('error');
}
}));
});
});
test_utils_1.given('verify() is called with retries=1', () => {
const options = { retries: 1, retryDelay: 0 };
let verifier;
beforeEach(() => {
verifier = jest.fn();
});
test_utils_1.when('any call to verifier() returns a valid verification', () => {
beforeEach(() => {
verifier.mockImplementation(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
return ({
description: 'attempt 1',
result: 'valid'
});
}));
});
test_utils_1.then('it should call verifier() once', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
yield verification_1.verify(verifier, driver, options);
expect(verifier.mock.calls.length).toBe(1);
expect(verifier.mock.calls[0][0]).toBe(driver);
expect(verifier.mock.calls[0][1]).toBe(1);
expect(verifier.mock.calls[0][2]).toBe(options.retries);
}));
test_utils_1.then('it should return the verification', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
const verification = yield verification_1.verify(verifier, driver, options);
expect(verification).toEqual({ description: 'attempt 1', result: 'valid' });
}));
});
for (const result of ['invalid', 'error']) {
const adjective = result === 'error' ? 'erroneous' : result;
test_utils_1.when(`any call to verifier() returns an ${adjective} verification`, () => {
beforeEach(() => {
verifier.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
return ({
description: 'attempt 1',
result
});
}));
verifier.mockImplementationOnce(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
return ({
description: 'attempt 2',
result
});
}));
});
test_utils_1.then('it should call verifier() twice', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
yield verification_1.verify(verifier, driver, options);
expect(verifier.mock.calls.length).toBe(2);
expect(verifier.mock.calls[0][0]).toBe(driver);
expect(verifier.mock.calls[0][1]).toBe(1);
expect(verifier.mock.calls[0][2]).toBe(options.retries);
expect(verifier.mock.calls[1][0]).toBe(driver);
expect(verifier.mock.calls[1][1]).toBe(2);
expect(verifier.mock.calls[1][2]).toBe(options.retries);
}));
test_utils_1.then('it should return the second verification', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
const verification = yield verification_1.verify(verifier, driver, options);
expect(verification).toEqual({ description: 'attempt 2', result });
}));
test_utils_1.then('it should delay the second call to verifier()', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
jest.useFakeTimers();
const retryDelay = 123;
const promise = verification_1.verify(verifier, driver, Object.assign({}, options, { retryDelay }));
yield test_utils_1.shortSleep();
expect(verifier.mock.calls.length).toBe(1);
jest.runTimersToTime(retryDelay - 1);
yield test_utils_1.shortSleep();
expect(verifier.mock.calls.length).toBe(1);
jest.runTimersToTime(1);
yield test_utils_1.shortSleep();
expect(verifier.mock.calls.length).toBe(2);
yield promise;
}
finally {
jest.useRealTimers();
}
}));
});
}
});
//# sourceMappingURL=verification.test.js.map