Skip to content

Commit

Permalink
re-write tests (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyhilliard authored Jun 30, 2022
1 parent 9042322 commit ed35be1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 78 deletions.
8 changes: 4 additions & 4 deletions .cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"billclinton",
"lumberjack",
"jimmyfalon",
"nailedit",
"naileditt",
"buttonmash",
"thor",
"bill",
Expand Down Expand Up @@ -80,7 +80,7 @@
"nachoagree",
"prettygood",
"okok",
"alright",
"alrightt",
"ehwellsee",
"wellseewhathappens",
"weregoodhere",
Expand Down Expand Up @@ -123,7 +123,7 @@
"let-me-know",
"vince-excited",
"seal-approval",
"ill-allow-it",
"ill-allow-itt",
"yessssss",
"kanye-approves",
"snape-approves",
Expand All @@ -135,7 +135,7 @@
"mad-respect",
"do-it",
"goooood",
"excellent",
"excellentt",
"everything-is-good",
"do-ittttt",
"ill-allow-it",
Expand Down
Empty file removed .prettierrc
Empty file.
122 changes: 52 additions & 70 deletions gif.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,109 +10,91 @@ const START_WITH_LOWER_CASE_REGEX = new RegExp(/^[a-z]/);
const MAX_SIZE_IN_BYTES = 5000000;

function isValidHttpUrl(string) {
let url;
let protocol;

try {
url = new URL(string);
protocol = new URL(string)?.protocol;
} catch (_) {
return false;
}

return url.protocol === 'http:' || url.protocol === 'https:';
return protocol === 'http:' || protocol === 'https:';
}

describe('Validating all GIFs', () => {
let allNames = new Set();
let existingURLs = new Set();
let allNames = new Set();
let existingURLs = new Set();

gifs.forEach(({ _id, name, url, description, active }, i) => {
allNames.add(name);
gifs.forEach(({ _id, name, url, description, active }, i) => {
allNames.add(name);

// Only pre-existing GIFs are tested here
if (_id < existingGifs.length) {
test(`${name}: Existing GIFs should not be modified`, () => {
// The existing names cannot be changed
expect(name).toBe(existingGifs[i]);
});

existingURLs.add(url);
describe(`Validating entry: '${name}'`, () => {
// Only newly added GIFs are tested here
if (_id > existingGifs.length - 1) {
const { host, search, hash } = urlUtil.parse(url);

// Only newly added GIFs are tested here
} else {
test(`${name}: New GIF URL doesnt already exist`, () => {
expect(existingURLs.has(url)).not.toBe(true);
});
test('GIF URL doesnt already exist', () => expect(existingURLs.has(url)).not.toBe(true));

test(`${name}: New GIF size is less than 5MB`, async () => {
test('GIF size is less than 5MB', async () => {
const { length: bytes } = await probe(url);
expect(bytes).toBeLessThan(MAX_SIZE_IN_BYTES);
});
}

// Every GIF (new or old) is tested below here
test(`${name}: GIF should be valid`, () => {
// The _id needs to be the same as its index in the array
expect(_id).toBe(i);
test('The _id needs to be the same as its index in the array', async () => expect(_id).toBe(i));

test('The _id needs to be a number', async () => {
expect(IS_NUMBER_REGEX.test(_id)).toBe(true);
expect(typeof _id).toBe('number');
expect(Number.isInteger(_id)).toBe(true);
});

// The _id needs to be a number
expect(IS_NUMBER_REGEX.test(_id)).toBe(true);
expect(typeof _id).toBe('number');
expect(Number.isInteger(_id)).toBe(true);
test('New names must be unique', async () => expect(existingGifs.includes(name)).not.toBe(true));

// New names must be unique
expect(name).not.toBe(existingGifs.includes(name));
test('The name is required', async () => expect(typeof name).toBe('string'));

// The name is required
expect(typeof name).toBe('string');
test('The name must be less than 50 characters', async () => expect(name.length).toBeLessThan(50));

// The name must be less than 50 characters
expect(name.length).toBeLessThan(50);
test('The name must be greater than 1 character', async () => expect(name.length).toBeGreaterThan(1));

// The name must be greater than 1 character
expect(name.length).toBeGreaterThan(1);
test('The name must only contain lowercase letters, numbers and dashes (-)', async () =>
expect(LOWER_CASE_REGEX.test(name)).toBe(true));

// The name must only contain lowercase letters, numbers and dashes (-)
expect(LOWER_CASE_REGEX.test(name)).toBe(true);
test('The name must start with a lowercase letter', async () =>
expect(START_WITH_LOWER_CASE_REGEX.test(name)).toBe(true));

// The name must start with a lowercase letter
expect(START_WITH_LOWER_CASE_REGEX.test(name)).toBe(true);
test('The url is required', async () => expect(typeof url).toBe('string'));

// The url is required
expect(typeof url).toBe('string');
test('The url must be less than 2000 characters', async () => expect(url.length).toBeLessThan(2000));

// The url must be less than 2000 characters
expect(url.length).toBeLessThan(2000);
test('the URL is a valid URL with a protocol', async () => expect(isValidHttpUrl(url)).toBe(true));

// the URL is a valid URL with a protocol
expect(isValidHttpUrl(url)).toBe(true);
test('The url domain must be an approved domain', async () =>
expect(domains.includes(host.substring(host.indexOf('.') + 1))).toBe(true));

// The url domain must be an approved domain
const { host } = urlUtil.parse(url);
expect(domains.includes(host.substring(host.indexOf('.') + 1))).toBe(true);
test('The url must not have query params', async () => expect(!!search).not.toBe(true));

// The url must not have query params
expect(!!urlUtil.parse(url).search).not.toBe(true);
test('The url must not have a fragment', async () => expect(!!hash).not.toBe(true));

// The url must not have a fragment
expect(!!urlUtil.parse(url).hash).not.toBe(true);
test('The description is required', async () => expect(typeof description).toBe('string'));

// The description is required
expect(typeof description).toBe('string');
test('The description cannot contain special characters', async () =>
expect(SPECIAL_CHAR_REGEX.test(description)).toBe(true));

// The description cannot contain special characters
expect(SPECIAL_CHAR_REGEX.test(description)).toBe(true);
test('The description must be less than 200 characters', async () =>
expect(description.length).toBeLessThan(200));

// The description must be less than 200 characters
expect(description.length).toBeLessThan(200);
test('The active state is required', async () => {
expect(active).toBeGreaterThan(-1);
expect(active).toBeLessThan(2);
});

// The active state is required
expect(active).toBeGreaterThan(-1);
expect(active).toBeLessThan(2);
});
});
// Only pre-existing GIFs are tested here
} else {
existingURLs.add(url);

test('The existing entries cannot be removed', () => {
const existingGIFsStillExist = existingGifs.every((name, i) => allNames.has(name));
expect(existingGIFsStillExist).toBe(true);
test('Existing GIFs should not be modified or removed', () => {
expect(name).toBe(existingGifs[i]);
expect(allNames.has(name)).toBe(true);
});
}
});
});
22 changes: 18 additions & 4 deletions gifs.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
},
{
"_id": 17,
"name": "nailedit",
"name": "naileditt",
"url": "https://media0.giphy.com/media/8VrtCswiLDNnO/giphy.gif",
"description": "Andy Bernard from the office saying nailed it",
"active": 1
Expand Down Expand Up @@ -562,7 +562,7 @@
},
{
"_id": 80,
"name": "alright",
"name": "alrightt",
"url": "https://c.tenor.com/zxxIt2fiDdkAAAAd/larry-david-curb-your-enthusiasm.gif",
"description": "alright alright",
"active": 1
Expand Down Expand Up @@ -863,7 +863,7 @@
},
{
"_id": 123,
"name": "ill-allow-it",
"name": "ill-allow-itt",
"url": "https://c.tenor.com/AALL2V5wziAAAAAC/yes-allow-it.gif",
"description": "Ill allow it",
"active": 1
Expand Down Expand Up @@ -947,7 +947,7 @@
},
{
"_id": 135,
"name": "excellent",
"name": "excellentt",
"url": "https://c.tenor.com/fadXuWxfW_AAAAAC/the-simpsons-mr-burns.gif",
"description": "excellentt",
"active": 1
Expand Down Expand Up @@ -1028,6 +1028,20 @@
"url": "https://media.giphy.com/media/bKBM7H63PIykM/giphy.gif",
"description": "well done sir ",
"active": 1
},
{
"_id": 147,
"name": "awesome-yes",
"url": "https://media.giphy.com/media/pK4JVcqh68lIo2dJ0V/giphy-downsized.gif",
"description": "Awesome yes will",
"active": 1
},
{
"_id": 148,
"name": "ahhhyeahh",
"url": "https://media.giphy.com/media/3ov9k9qBiR17LYWPEQ/giphy.gif",
"description": "ahhhh yeahhh",
"active": 1
}
]
}

0 comments on commit ed35be1

Please sign in to comment.