From 683e1ceda049dd5be0b2f4e17ad3fd1ac94a1017 Mon Sep 17 00:00:00 2001 From: Rajesh M R Date: Wed, 6 Oct 2021 14:19:30 +0530 Subject: [PATCH 1/2] Adds test to check for duplicates in data.json Duplicates texts and their frequency are printed in console --- data/data.json | 16 ------- test/test.js | 114 ++++++++++++++++++++++++++++++------------------- 2 files changed, 71 insertions(+), 59 deletions(-) diff --git a/data/data.json b/data/data.json index 370461db0..377403b04 100644 --- a/data/data.json +++ b/data/data.json @@ -323,10 +323,6 @@ "text":"I’m not afraid of dying, I’m afraid of not trying.", "from":"Jay Z, musician." }, - { - "text":"Whatever the mind can conceive and believe, the mind can achieve.", - "from":"Dr. Napoleon Hill, author of Think and Grow Rich." - }, { "text":"The longer you’re not taking action the more money you’re losing", "from":"Carrie Wilkerson" @@ -499,10 +495,6 @@ "text":"Failure defeats losers, failure inspires winners.", "from":"Robert T. Kiyosaki" }, - { - "text":"I have not failed. I’ve just found 10,000 ways that won’t work.", - "from":"Thomas Edison" - }, { "text":"The biggest failure you can have in life is not trying at all.", "from":"Emil Motycka" @@ -679,10 +671,6 @@ "text":"If you’re not making mistakes, then you’re not making decisions.", "from":"Catherine Cook" }, - { - "text":"Your most unhappy customers are your greatest source of learning.", - "from":"Bill Gates" - }, { "text":"One can get anything if he is willing to help enough others get what they want.", "from":"Zig Ziglar" @@ -939,10 +927,6 @@ "text":"A man must be big enough to admit his mistakes, smart enough to profit from them, and strong enough to correct them.", "from":"John C. Maxwell" }, - { - "text":"In the modern world of business, it is useless to be a creative, original thinker unless you can also sell what you create.", - "from":"David Ogilvy" - }, { "text":"Success is doing what you want, where you want, when you want, with whom you want as much as you want.", "from":"Tony Robbins" diff --git a/test/test.js b/test/test.js index bc167e134..89f0d8e26 100644 --- a/test/test.js +++ b/test/test.js @@ -1,46 +1,74 @@ -const expect = require('chai').expect, - { getQuote, getRandomQuote } = require('../lib/index'); +const expect = require("chai").expect, + { getQuote, getRandomQuote } = require("../lib/index"); -describe('Running tests for methods of inspirational-quotes', () => { - describe('getQuote', () => { - it('should return an object', (done) => { - const randomQuote = getQuote(); - expect(randomQuote).to.be.an('object'); - done(); - }); - it('should contain text and author', (done) => { - const randomQuote = getQuote(); - expect(randomQuote).to.have.property('author'); - expect(randomQuote).to.have.property('text'); - done(); - }); - it('should not contain empty text', (done) => { - const randomQuote = getQuote(); - expect(randomQuote.text).to.not.be.empty; - done(); - }); - it('should not contain empty author', (done) => { - const randomQuote = getQuote(); - expect(randomQuote.author).to.not.be.empty; - done(); - }); - it('should not return author field if author: false is passed', (done) => { - const randomQuote = getQuote({author: false}); - expect(randomQuote).to.not.have.property('author'); - expect(randomQuote).to.have.property('text'); - done(); - }); - }); - describe('getRandomQuote', () => { - it('should return a string', (done) => { - const randomQuote = getRandomQuote(); - expect(randomQuote).to.be.an('string'); - done(); - }); - it('should have length greater than 5', (done) => { - const randomQuote = getRandomQuote(); - expect(randomQuote).to.have.length.greaterThan(5); - done(); - }); +allQuotes = require("../data/data.json"); + +describe("Running tests for methods of inspirational-quotes", () => { + describe("getQuote", () => { + it("should return an object", (done) => { + const randomQuote = getQuote(); + expect(randomQuote).to.be.an("object"); + done(); + }); + it("should contain text and author", (done) => { + const randomQuote = getQuote(); + expect(randomQuote).to.have.property("author"); + expect(randomQuote).to.have.property("text"); + done(); + }); + it("should not contain empty text", (done) => { + const randomQuote = getQuote(); + expect(randomQuote.text).to.not.be.empty; + done(); + }); + it("should not contain empty author", (done) => { + const randomQuote = getQuote(); + expect(randomQuote.author).to.not.be.empty; + done(); + }); + it("should not return author field if author: false is passed", (done) => { + const randomQuote = getQuote({ author: false }); + expect(randomQuote).to.not.have.property("author"); + expect(randomQuote).to.have.property("text"); + done(); + }); + }); + describe("getRandomQuote", () => { + it("should return a string", (done) => { + const randomQuote = getRandomQuote(); + expect(randomQuote).to.be.an("string"); + done(); + }); + it("should have length greater than 5", (done) => { + const randomQuote = getRandomQuote(); + expect(randomQuote).to.have.length.greaterThan(5); + done(); + }); + }); + describe("data.json", () => { + it("should not have any duplicate quotes", (done) => { + let duplicates = [ + ...allQuotes + .reduce((accumulator, current) => { + let text = current.text; + accumulator.set(text, (accumulator.get(text) || 0) + 1); + return accumulator; + }, new Map()) + .entries(), + ] + .filter(([_, occurance]) => occurance > 1) + .map(([text, occurance]) => ({ + text, + occurance, + })); + + if(duplicates.length>0){ + console.log(duplicates); + } + + const hasDuplicates = duplicates.length > 0; + expect(hasDuplicates).to.be.false; + done(); }); + }); }); From e81f9433d57c428f70f6b342557fc31ea441a62f Mon Sep 17 00:00:00 2001 From: Rajesh M R Date: Wed, 6 Oct 2021 14:22:29 +0530 Subject: [PATCH 2/2] Fixed formatting --- test/test.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/test.js b/test/test.js index 89f0d8e26..c98af01c0 100644 --- a/test/test.js +++ b/test/test.js @@ -1,52 +1,52 @@ -const expect = require("chai").expect, - { getQuote, getRandomQuote } = require("../lib/index"); +const expect = require('chai').expect, + { getQuote, getRandomQuote } = require('../lib/index'); -allQuotes = require("../data/data.json"); +allQuotes = require('../data/data.json'); -describe("Running tests for methods of inspirational-quotes", () => { - describe("getQuote", () => { - it("should return an object", (done) => { +describe('Running tests for methods of inspirational-quotes', () => { + describe('getQuote', () => { + it('should return an object', (done) => { const randomQuote = getQuote(); - expect(randomQuote).to.be.an("object"); + expect(randomQuote).to.be.an('object'); done(); }); - it("should contain text and author", (done) => { + it('should contain text and author', (done) => { const randomQuote = getQuote(); - expect(randomQuote).to.have.property("author"); - expect(randomQuote).to.have.property("text"); + expect(randomQuote).to.have.property('author'); + expect(randomQuote).to.have.property('text'); done(); }); - it("should not contain empty text", (done) => { + it('should not contain empty text', (done) => { const randomQuote = getQuote(); expect(randomQuote.text).to.not.be.empty; done(); }); - it("should not contain empty author", (done) => { + it('should not contain empty author', (done) => { const randomQuote = getQuote(); expect(randomQuote.author).to.not.be.empty; done(); }); - it("should not return author field if author: false is passed", (done) => { + it('should not return author field if author: false is passed', (done) => { const randomQuote = getQuote({ author: false }); - expect(randomQuote).to.not.have.property("author"); - expect(randomQuote).to.have.property("text"); + expect(randomQuote).to.not.have.property('author'); + expect(randomQuote).to.have.property('text'); done(); }); }); - describe("getRandomQuote", () => { - it("should return a string", (done) => { + describe('getRandomQuote', () => { + it('should return a string', (done) => { const randomQuote = getRandomQuote(); - expect(randomQuote).to.be.an("string"); + expect(randomQuote).to.be.an('string'); done(); }); - it("should have length greater than 5", (done) => { + it('should have length greater than 5', (done) => { const randomQuote = getRandomQuote(); expect(randomQuote).to.have.length.greaterThan(5); done(); }); }); - describe("data.json", () => { - it("should not have any duplicate quotes", (done) => { + describe('data.json', () => { + it('should not have any duplicate quotes', (done) => { let duplicates = [ ...allQuotes .reduce((accumulator, current) => {