Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created January 27, 2022 02:35
Show Gist options
  • Save natafaye/18b581e46700228ccc7258e6b18472f2 to your computer and use it in GitHub Desktop.
Save natafaye/18b581e46700228ccc7258e6b18472f2 to your computer and use it in GitHub Desktop.
class AddressBook {
constructor() {
this.entries = [];
}
// get all your entries
getAll() {
}
// get all entries by type
getAllByType(type) {
// const entriesWithType = [];
// for(const entry of this.entries) {
// if(entry.type === type) {
// entriesWithType.push(entry);
// }
// }
// return entriesWithType;
return this.entries.filter( entry => entry.type === type );
}
// find an entry by name and type
getByNameAndType(name, type) {
return this.entries.find( entry => entry.name === name && entry.type === type );
}
// delete all entries
// add entries
addEntry(name, type) {
// const potentialDuplicate = this.entries.find( entry => entry.name === name && entry.type === type );
// if(potentialDuplicate) {
// return;
// }
if( this.getByNameAndType(name, type) ) {
return;
}
// for(const entry of this.entries) {
// if(entry.name === name && entry.type === type) {
// return;
// }
// }
// let foundDuplicate = false;
// this.entries.forEach( entry => {
// if(entry.name === name && entry.type === type) {
// foundDuplicate = true;
// }
// })
// if(foundDuplicate) {
// return;
// }
const newEntry = {
name: name,
type: type
}
this.entries.push(newEntry);
}
// remove entries
}
const expect = chai.expect;
describe("AddressBook", function() {
describe("#getAll()", function() {
it("should get all the entries", function() {
})
})
describe("#getAllByType()", function() {
it("should get all entries with that type", function() {
const book = new AddressBook();
book.addEntry("Natalie", "teacher");
book.addEntry("Dylan", "teacher");
book.addEntry("Joshua", "butcher");
book.addEntry("Raven", "baker");
const teachers = book.getAllByType("teacher");
expect(teachers).to.have.lengthOf(2);
expect(teachers[0].name).to.equal("Natalie")
expect(teachers[0].type).to.equal("teacher")
expect(teachers[1].name).to.equal("Dylan")
expect(teachers[1].type).to.equal("teacher")
})
})
describe("#addEntry()", function() {
it("should add each entry we give it with the correct data", function() {
const book = new AddressBook();
book.addEntry("Natalie", "teacher");
book.addEntry("Dylan", "teacher");
expect(book.entries).to.have.lengthOf(2);
expect(book.entries[0].name).to.equal("Natalie")
expect(book.entries[0].type).to.equal("teacher")
expect(book.entries[1].name).to.equal("Dylan")
expect(book.entries[1].type).to.equal("teacher")
})
it("should not add duplicates", function() {
const book = new AddressBook();
book.addEntry("Natalie", "teacher");
book.addEntry("Dylan", "teacher");
book.addEntry("Natalie", "teacher");
expect(book.entries).to.have.lengthOf(2);
expect(book.entries[0].name).to.equal("Natalie")
expect(book.entries[0].type).to.equal("teacher")
expect(book.entries[1].name).to.equal("Dylan")
expect(book.entries[1].type).to.equal("teacher")
})
})
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"><p><a href=".">Index</a></p></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script>
mocha.setup('bdd');
</script>
<script src="AddressBook.js"></script>
<script src="AddressBook.test.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment