record.js/test/record.spec.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-05-06 22:28:53 +00:00
const test = require("tape");
2021-01-02 09:48:43 +00:00
const Record = require("../dist/record.cjs.js").default;
2018-04-02 04:35:22 +00:00
2018-05-06 22:28:53 +00:00
test("Record.js", function(t) {
2018-04-02 04:35:22 +00:00
let pets;
2018-05-06 22:28:53 +00:00
t.test("setup", function(t) {
2018-04-02 04:35:22 +00:00
pets = new Record();
2018-05-06 22:28:53 +00:00
t.ok(Record, "Record should exist");
t.ok(pets, "new collection should have been created");
2018-04-02 04:35:22 +00:00
t.end();
});
2019-01-13 19:15:11 +00:00
2018-05-06 22:28:53 +00:00
t.test("should add 3 records one-by-one", function(t) {
t.ok(pets.add, "add method exists");
2019-01-13 19:15:11 +00:00
2018-04-02 04:35:22 +00:00
let plato = pets.add({"name": "plato", "type": "dog"});
let socrates = pets.add({"id": "1", "name": "socrates", "type": "dog"});
let hypatia = pets.add({"name": "hypatia", "type": "cat"});
2019-01-13 19:15:11 +00:00
2018-05-06 22:28:53 +00:00
t.equal(plato.name, "plato", "plato should have been added");
t.equal(socrates.name, "socrates", "socrates should have been added");
t.equal(hypatia.name, "hypatia", "hypatia should have been added");
2019-01-13 19:15:11 +00:00
2018-05-06 22:28:53 +00:00
t.ok(plato.id, "platos record id should be auto-generated");
t.equal(socrates.id, "1","socrates record id should not be auto-generated");
2018-04-02 04:35:22 +00:00
t.end();
});
2018-05-06 22:28:53 +00:00
t.test("should be 3 records", function(t) {
t.equal(pets.find().length, 3);
2018-04-02 04:35:22 +00:00
t.end();
});
2019-06-16 08:55:25 +00:00
t.test("should be able to update a record", function(t) {
let plato = pets.find({"name": "plato"})[0];
t.equal(plato.age, undefined, "should not have age yet");
plato.age = 80;
let update = pets.update(plato);
t.equal(update.age, 80, "should exist in db now");
t.end();
});
t.test("should be able find pet", function(t) {
let plato = pets.find({"age": 80})[0];
t.ok(plato, "found plato");
t.equal(plato.name, "plato", "yep, that him");
t.end();
});
2018-04-02 04:35:22 +00:00
t.test("should find all matching records", function(t) {
let dogs = pets.find({"type": "dog"});
t.equal(dogs.length, 2, "should be 2 dogs in the house");
2018-05-06 22:28:53 +00:00
t.equal(pets.find().length, 3, "but should still have 3 pets");
2018-04-02 04:35:22 +00:00
t.end();
});
2018-05-06 22:28:53 +00:00
t.test("should be able to remove a record", function() {
2018-04-02 04:35:22 +00:00
let hypatia = pets.remove({"name": "hypatia"});
2018-05-06 22:28:53 +00:00
t.equal(hypatia[0].name, "hypatia", "hypatia should be removed");
2018-04-02 04:35:22 +00:00
2018-05-06 22:28:53 +00:00
t.equal(pets.find().length, 2, "yes, hypatia has left the building");
2019-01-13 19:15:11 +00:00
2018-04-02 04:35:22 +00:00
t.end();
});
});