Skip to content

Commit 4c3249e

Browse files
committed
Merge pull request #67 from devlucky/database/first-last
Test Database.first and Database.last better
2 parents 6b52350 + bf2c404 commit 4c3249e

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kakapo",
3-
"version": "0.0.0-semantically-released",
3+
"version": "0.0.0-semantic-release",
44
"description": "Next generation mocking library in Javascript",
55
"main": "src/",
66
"scripts": {

src/Database/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ export class Database {
8989
}
9090

9191
first(collectionName) {
92+
this.checkFactoryPresence(collectionName);
9293
return this.all(collectionName)[0];
9394
}
9495

9596
last(collectionName) {
97+
this.checkFactoryPresence(collectionName);
9698
return lastItem(this.all(collectionName));
9799
}
98100

test/specs/database_spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,44 @@ export const databaseSpec = () => {
182182
assert.end();
183183
});
184184

185+
test('DB # first', (assert) => {
186+
const db = new Database();
187+
188+
db.register('user', userFactory);
189+
db.create('user', 5);
190+
191+
const user = db.first('user');
192+
193+
assert.ok(_.isObject(user), 'returns first entity of collection.');
194+
assert.equal(user.id, 0, 'returns correct first entity of collection.');
195+
196+
assert.doesNotThrow(() => db.first('user'),
197+
'Doesn\'t throw error when collection is present.');
198+
assert.throws(() => db.first('game'),
199+
'Throws error when collection is not present.');
200+
201+
assert.end();
202+
});
203+
204+
test('DB # last', (assert) => {
205+
const db = new Database();
206+
207+
db.register('user', userFactory);
208+
db.create('user', 5);
209+
210+
const user = db.last('user');
211+
212+
assert.ok(_.isObject(user), 'returns last entity of collection.');
213+
assert.equal(user.id, 4, 'returns correct last entity of collection.');
214+
215+
assert.doesNotThrow(() => db.last('user'),
216+
'Doesn\'t throw error when collection is present.');
217+
assert.throws(() => db.last('game'),
218+
'Throws error when collection is not present.');
219+
220+
assert.end();
221+
});
222+
185223
test('DB # push', (assert) => {
186224
const db = new Database();
187225

0 commit comments

Comments
 (0)