-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds findOrCreateBy to schema (#1611)
- Loading branch information
1 parent
dba7607
commit 1159d39
Showing
2 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Schema from 'ember-cli-mirage/orm/schema'; | ||
import Model from 'ember-cli-mirage/orm/model'; | ||
import Db from 'ember-cli-mirage/db'; | ||
import {module, test} from 'qunit'; | ||
|
||
module('Integration | ORM | #findOrCreateBy', function(hooks) { | ||
hooks.beforeEach(function() { | ||
let db = new Db({ users: [ | ||
{ id: 1, name: 'Link', good: true }, | ||
{ id: 2, name: 'Zelda', good: true }, | ||
{ id: 3, name: 'Ganon', good: false } | ||
] }); | ||
|
||
this.User = Model.extend(); | ||
|
||
this.schema = new Schema(db, { | ||
user: this.User | ||
}); | ||
}); | ||
|
||
test('it returns the first model that matches the attrs', function(assert) { | ||
let user = this.schema.users.findOrCreateBy({ good: true }); | ||
|
||
assert.ok(user instanceof this.User); | ||
assert.deepEqual(user.attrs, { id: '1', name: 'Link', good: true }); | ||
}); | ||
|
||
test('it creates a model if no existing model with the attrs is found', function(assert) { | ||
assert.equal(this.schema.db.users.length, 3); | ||
|
||
let newUser = this.schema.users.findOrCreateBy({ name: 'Link', good: false }); | ||
|
||
assert.equal(this.schema.db.users.length, 4); | ||
assert.ok(newUser instanceof this.User); | ||
assert.deepEqual(newUser.attrs, { id: '4', name: 'Link', good: false }); | ||
}); | ||
|
||
// test('it returns models that match using a query function', function(assert) { | ||
// let users = schema.users.where(function(rec) { | ||
// return !rec.good; | ||
// }); | ||
// | ||
// assert.ok(users instanceof Collection, 'it returns a collection'); | ||
// assert.equal(users.models.length, 1); | ||
// assert.ok(users.models[0] instanceof User); | ||
// assert.deepEqual(users.models[0].attrs, { id: '3', name: 'Ganon', good: false }); | ||
// }); | ||
|
||
// test('it returns an empty collection if no models match a query', function(assert) { | ||
// let users = schema.users.where({ name: 'Link', good: false }); | ||
// | ||
// assert.ok(users instanceof Collection, 'it returns a collection'); | ||
// assert.equal(users.models.length, 0); | ||
// }); | ||
}); |