From ed35be15cf3caaea6c1b41ddfdd87e53abb4a8b5 Mon Sep 17 00:00:00 2001 From: Riley Hilliard Date: Thu, 30 Jun 2022 13:02:57 -0700 Subject: [PATCH] re-write tests (#52) --- .cache.json | 8 ++-- .prettierrc | 0 gif.test.js | 122 ++++++++++++++++++++++------------------------------ gifs.json | 22 ++++++++-- 4 files changed, 74 insertions(+), 78 deletions(-) delete mode 100644 .prettierrc diff --git a/.cache.json b/.cache.json index 83c7479..97d76e8 100644 --- a/.cache.json +++ b/.cache.json @@ -17,7 +17,7 @@ "billclinton", "lumberjack", "jimmyfalon", - "nailedit", + "naileditt", "buttonmash", "thor", "bill", @@ -80,7 +80,7 @@ "nachoagree", "prettygood", "okok", - "alright", + "alrightt", "ehwellsee", "wellseewhathappens", "weregoodhere", @@ -123,7 +123,7 @@ "let-me-know", "vince-excited", "seal-approval", - "ill-allow-it", + "ill-allow-itt", "yessssss", "kanye-approves", "snape-approves", @@ -135,7 +135,7 @@ "mad-respect", "do-it", "goooood", - "excellent", + "excellentt", "everything-is-good", "do-ittttt", "ill-allow-it", diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index e69de29..0000000 diff --git a/gif.test.js b/gif.test.js index 91020ec..517544b 100644 --- a/gif.test.js +++ b/gif.test.js @@ -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); + }); + } }); }); diff --git a/gifs.json b/gifs.json index c5a404b..f67a48a 100644 --- a/gifs.json +++ b/gifs.json @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 } ] }