scratch-www/test/unit/lib/fastly-extended.test.js

161 lines
5 KiB
JavaScript
Raw Permalink Normal View History

describe('fastly library', () => {
2019-09-26 10:49:12 -04:00
let mockedFastlyRequest = {};
2019-09-26 10:49:12 -04:00
jest.mock('fastly', () => (() => ({
request: mockedFastlyRequest
})));
const fastlyExtended = require('../../../bin/lib/fastly-extended'); // eslint-disable-line global-require
test('getLatestActiveVersion returns largest active VCL number, ' +
'when called with VCLs in sequential order', done => {
mockedFastlyRequest = jest.fn((method, url, cb) => {
cb(null, [
{
number: 1,
active: false
},
{
number: 2,
active: false
},
{
number: 3,
active: true
},
{
number: 4,
active: false
}
]);
});
const fastlyInstance = fastlyExtended('api_key', 'service_id');
fastlyInstance.getLatestActiveVersion((err, response) => {
expect(err).toBe(null);
expect(response).toEqual({
number: 3,
active: true
});
expect(mockedFastlyRequest).toHaveBeenCalledWith(
'GET', '/service/service_id/version', expect.any(Function)
);
done();
});
});
test('getLatestActiveVersion returns largest active VCL number, when called with VCLs in mixed up order', done => {
mockedFastlyRequest = jest.fn((method, url, cb) => {
cb(null, [
{
number: 4,
active: false
},
{
number: 1,
2019-09-26 10:49:12 -04:00
active: false
},
{
number: 2,
active: true
},
{
number: 3,
active: false
}
]);
});
2019-09-26 10:49:12 -04:00
const fastlyInstance = fastlyExtended('api_key', 'service_id');
2019-09-26 10:49:12 -04:00
fastlyInstance.getLatestActiveVersion((err, response) => {
expect(err).toBe(null);
expect(response).toEqual({
number: 2,
active: true
});
expect(mockedFastlyRequest).toHaveBeenCalledWith(
'GET', '/service/service_id/version', expect.any(Function)
);
done();
});
});
2019-09-26 10:49:12 -04:00
test('getLatestActiveVersion returns null, when none of the VCL versions are active', done => {
mockedFastlyRequest = jest.fn((method, url, cb) => {
cb(null, [
{
number: 4,
active: false
},
{
number: 1,
active: false
},
{
number: 2,
active: false
},
{
number: 3,
active: false
}
]);
});
const fastlyInstance = fastlyExtended('api_key', 'service_id');
fastlyInstance.getLatestActiveVersion((err, response) => {
expect(err).toBe(null);
expect(response).toEqual(null);
expect(mockedFastlyRequest).toHaveBeenCalledWith(
'GET', '/service/service_id/version', expect.any(Function)
);
done();
});
});
test('getLatestActiveVersion returns largest active VCL number, ' +
'when called with a single active VCL', done => {
mockedFastlyRequest = jest.fn((method, url, cb) => {
cb(null, [
{
number: 1,
active: true
}
]);
});
const fastlyInstance = fastlyExtended('api_key', 'service_id');
fastlyInstance.getLatestActiveVersion((err, response) => {
expect(err).toBe(null);
expect(response).toEqual({
2019-09-26 10:49:12 -04:00
number: 1,
active: true
});
expect(mockedFastlyRequest).toHaveBeenCalledWith(
'GET', '/service/service_id/version', expect.any(Function)
);
done();
});
});
2019-09-26 10:49:12 -04:00
test('getLatestActiveVersion returns null, when called with a single inactive VCL', done => {
mockedFastlyRequest = jest.fn((method, url, cb) => {
cb(null, [
{
number: 1,
active: false
}
]);
});
const fastlyInstance = fastlyExtended('api_key', 'service_id');
fastlyInstance.getLatestActiveVersion((err, response) => {
expect(err).toBe(null);
expect(response).toEqual(null);
expect(mockedFastlyRequest).toHaveBeenCalledWith(
'GET', '/service/service_id/version', expect.any(Function)
);
done();
});
});
});