diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..0bfc0ef9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ + +# Test files +duplicates.json \ No newline at end of file diff --git a/README.md b/README.md index e28c159f2..b1b03d7f8 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,20 @@ Then run the **demo.js**: $ node demo.js ``` +## Tests + +Issue #257 + +### Duplicate quotes + +Duplicate quotes can be found by running: + +``` +npm run test:duplicates +``` + +If any duplicate quotes are found, they will be output to a `duplicates.json` file to review. This only looks for duplicate quotes, not authors. + ## Related Works [![Quotter](https://img.shields.io/badge/GitHub%20Repo-Quotter-follow.svg?logo=github&style=social)](https://github.com/vinitshahdeo/Quotter) [![Twitter Follow](https://img.shields.io/twitter/follow/SayHiToQuotter.svg?style=social&logoColor=teal)](https://twitter.com/SayHiToQuotter) diff --git a/package.json b/package.json index ed3209bfd..c74f84e71 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "A simple NPM Package which returns Inspirational Quotes. Get your daily quote and stay motivated!", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha", + "test:duplicates": "mocha test/duplicates.js" }, "repository": { "type": "git", @@ -25,6 +26,10 @@ }, "directories": { "example": "examples", - "data":"data" + "data": "data" + }, + "devDependencies": { + "chai": "^4.2.0", + "mocha": "^6.2.2" } } diff --git a/test/duplicates.js b/test/duplicates.js new file mode 100644 index 000000000..b3159af7c --- /dev/null +++ b/test/duplicates.js @@ -0,0 +1,28 @@ +const fs = require('fs') +const expect = require('chai').expect; +const inputQuotesJson = require('../data/data.json') + +const formatQuote = (quote) => { + return quote.replace(/\W/g, '').toLowerCase().trim(); +} + +describe('Quotes', () => { + const duplicateQuotes = []; + it('Checks for duplicate quotes', () => { + const quoteSet = new Set(); + inputQuotesJson.map(mockQuote => { + let setSize = quoteSet.size; + const formattedQuote = formatQuote(mockQuote.text); + quoteSet.add(formattedQuote); + if(setSize === quoteSet.size) { + duplicateQuotes.push(mockQuote) + } + }); + + fs.writeFile('duplicates.json', JSON.stringify(duplicateQuotes), 'utf8', (err) => { + if (err) console.log(err) + }); + + expect(quoteSet.size).to.equal(inputQuotesJson.length); + }) +})