Skip to content
Permalink
Browse files

Test remaining statements for 100% test coverage.

Add tests for each module, and slightly update some modules, to achieve
100% tests coverage for all statements.
  • Loading branch information
skunkmb committed Jan 7, 2019
1 parent 41a9818 commit b3f249a10aa24bac760aef1afe5d08c5af4a2acd
@@ -162,6 +162,8 @@ let getMapArray = async function getLatitudeLongitudeArray(views) {
});
});

// `iplocation` returns `null` if the location is unavailable.
// See https://git.io/fhG6s.
if (
!lookup
|| lookup.latitude === null
@@ -39,7 +39,7 @@ module.exports.create = async function createShortLink(longLink, options) {
doc = await docRef.get();
} catch (error) {
console.error(error);
throw 'An unexpected error occurred when shortening the link';
throw 'An unexpected error occurred when shortening the link.';
}

if (!doc.exists) {
@@ -33,6 +33,14 @@ module.exports.addView = async function addToTotalViews(

await db.runTransaction(async (transaction) => {
let doc = await transaction.get(totalViewsRef);

if (!doc.exists) {
transaction.set(totalViewsRef, {
totalViewCount: 1,
});
return;
}

transaction.update(totalViewsRef, {
totalViewCount: (doc.data().totalViewCount || 0) + 1,
});
@@ -113,6 +113,40 @@ describe('link-statistics', () => {
assert.strictEqual(allTimeMap[i].longitude, 100);
}
});

it ('should not fail if no location is found', async () => {
mock('iplocation', {
default: (ipAddress, providers, callback) => {
callback('Example error');
},
});

linkStatistics = util.require('../server/link-statistics.js');

await assert.doesNotReject(linkStatistics.getEach());
});

it ('should not include an empty point', async () => {
mock('iplocation', {
default: (ipAddress, providers, callback) => {
if (ipAddress === 'abc' || ipAddress === 'def') {
callback(
undefined,
{ latitude: null, longitude: null },
);
return;
}

callback(undefined, { latitude: 1, longitude: 2 });
},
});

linkStatistics = util.require('../server/link-statistics.js');

let eachStatistic = await linkStatistics.getEach();
let mapPoints = eachStatistic[3]
assert.strictEqual(mapPoints.length, 4);
});
});

after(() => {
@@ -45,6 +45,16 @@ describe('long-links', () => {
'example.com',
);
});

it('should throw "unexpected" if the database fails', async () => {
util.mockDatabaseFail('Example error');

let longLinks = util.require('../server/long-links.js');
await assert.rejects(
longLinks.get('example'),
/^An unexpected error occurred when finding the short link\.$/
);
});
});

after(() => {
@@ -58,7 +58,6 @@ describe('short-links', () => {
it('should add the correct data to the database', async () => {
util.mockDatabaseDocGetSet(
{ exists: false },
undefined,
(data) => {
assert.strictEqual(data.longLink, 'http://example.com');
assert.strictEqual(data.viewCount, 0);
@@ -76,7 +75,6 @@ describe('short-links', () => {
async () => {
util.mockDatabaseDocGetSet(
{ exists: false },
undefined,
(data) => {
assert.strictEqual(
data.longLink,
@@ -103,7 +101,6 @@ describe('short-links', () => {
async () => {
util.mockDatabaseDocGetSet(
{ exists: false },
undefined,
(data) => {
assert.strictEqual(
data.longLink,
@@ -137,7 +134,6 @@ describe('short-links', () => {
async () => {
util.mockDatabaseDocGetSet(
{ exists: false },
undefined,
(data) => {
assert.strictEqual(
data.longLink,
@@ -160,6 +156,90 @@ describe('short-links', () => {
});
}
);

it('should fail if the long link is reserved', async () => {
mock('../server/config/settings.json', {
reservedLongLinkRegExps: ['^example.com$']
});

let shortLinks = util.require('../server/short-links.js');

await assert.rejects(
shortLinks.create('example.com'),
/^That long link cannot be shortened\.$/,
);
});

it('should fail if the custom short link is reserved', async () => {
mock('../server/config/settings.json', {
reservedLongLinkRegExps: [],
reservedCustomShortLinkIDRegExps: ['^exampleCustom$']
});

let shortLinks = util.require('../server/short-links.js');

await assert.rejects(
shortLinks.create('example.com', {
customShortLinkID: 'exampleCustom'
}),
/^That custom short link is unavailable\.$/,
);
});

it('should retry if the shortlink is taken', async () => {
mock('../server/database.js', {
get: () => ({
collection: () => ({
doc: (name) => ({
get: () => {
if (name === 'example-untaken') {
return { exists: false };
}

return { exists: true };
},
set: () => {
assert.equal(name, 'example-untaken');
},
})
})
})
});

let hasTriedOnce = false;
mock('../server/util.js', {
getRandomCharacters: () => {
if (hasTriedOnce) {
return 'example-untaken';
}

hasTriedOnce = true;
return 'example-taken';
}
});

let shortLinks = util.require('../server/short-links.js');
let shortLinkID = await shortLinks.create('example.com', {});
assert.equal(shortLinkID, 'example-untaken');
});

it('should throw "unexpected" if the database fails', async () => {
util.mockDatabaseFail('Example error');

let shortLinks = util.require('../server/short-links.js');
await assert.rejects(
shortLinks.create('example.com', {}),
/^An unexpected error occurred when shortening the link\.$/
);

util.mockDatabaseSetFail({ exists: false}, 'Example error');

shortLinks = util.require('../server/short-links.js');
await assert.rejects(
shortLinks.create('example.com', {}),
/^An unexpected error occurred when saving the shortlink\.$/
);
});
});

describe('addView', () => {
@@ -172,6 +252,16 @@ describe('short-links', () => {

await shortLinks.addView('example', 'abc');
});

it('should throw "unexpected" if the database fails', async () => {
util.mockDatabaseSubFail('Example doc error');

let shortLinks = util.require('../server/short-links.js');
await assert.rejects(
shortLinks.addView('example'),
/^An unexpected error occurred when updating the statistics\.$/
);
});
});

describe('getViews', () => {
@@ -231,8 +321,84 @@ describe('short-links', () => {
/^Statistics are unavailable for that link\.$/,
)
});

it('should return an empty array if there are no views', async () => {
util.mockDatabaseSubGet(
{ exists: true },
{ hideStatistics: false },
{ empty: true },
[]
);

let shortLinks = util.require('../server/short-links.js');

let views = await shortLinks.getViews('example', 'abc');
assert.equal(views.length, 0);
});

it('should throw "unexpected" if the database fails', async () => {
util.mockDatabaseFail('Example doc error');

let shortLinks = util.require('../server/short-links.js');
await assert.rejects(
shortLinks.getViews('example'),
/^An unexpected error occurred when getting the statistics\.$/
);

util.mockDatabaseSubFail('Example collection error');

shortLinks = util.require('../server/short-links.js');
await assert.rejects(
shortLinks.getViews('example'),
/^An unexpected error occurred when getting the statistics\.$/
);
});

it('should fail if the statistics do not exist', async () => {
util.mockDatabaseDoc({ exists: false });
let shortLinks = util.require('../server/short-links.js');

await assert.rejects(
shortLinks.getViews('example'),
/^Statistics are unavailable for that link\.$/
);
});
});

describe('getCreationDate', () => {
it('should return the correct creation date', async () => {
let creationDate = new Date('January 1, 2000');
util.mockDatabaseDoc(
{ exists: true },
{ created: util.getMockTimestamp(creationDate) },
);

let shortLinks = util.require('../server/short-links.js');

let returnedCreationDate = await shortLinks.getCreationDate('examle');
assert.equal(returnedCreationDate, creationDate);
});

it('should fail if the shortlink does not exist', async () => {
util.mockDatabaseDoc({ exists: false });
let shortLinks = util.require('../server/short-links.js');

assert.rejects(
shortLinks.getCreationDate('example'),
/^That short link does not exist\.$/,
);
});

it('should throw "unexpected" if the database fails', async () => {
util.mockDatabaseFail('Example error');
let shortLinks = util.require('../server/short-links.js');

await assert.rejects(
shortLinks.getCreationDate('example'),
/^An unexpected error occurred when getting the statistics\.$/
);
});
});
after(() => {
util.end();
});

0 comments on commit b3f249a

Please sign in to comment.
You can’t perform that action at this time.