Skip to content

Commit

Permalink
schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igorT committed Aug 12, 2019
1 parent 38fd696 commit 353cfcd
Showing 1 changed file with 111 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import { InvalidError } from 'ember-data/adapters/errors';
import JSONAPIAdapter from 'ember-data/adapters/json-api';
import JSONAPISerializer from 'ember-data/serializers/json-api';
import JSONSerializer from 'ember-data/serializers/json';
import { attr, hasMany, belongsTo } from '@ember-decorators/data';
import DSattr from 'ember-data/attr';
import { recordDataFor } from 'ember-data/-private';
import { recordDataFor, Snapshot } from 'ember-data/-private';
import Store from 'ember-data/store';
import { CUSTOM_MODEL_CLASS } from '@ember-data/canary-features';
import CoreStore from '@ember-data/store/-private/system/core-store';
Expand Down Expand Up @@ -157,26 +156,129 @@ if (CUSTOM_MODEL_CLASS) {
let person = store.createRecord('person', { name: 'chris' });
});

test('schema definition', function(assert) {
test('attribute and relationship with custom schema definition', async function(assert) {
assert.expect(10);
this.owner.register(
'adapter:application',
JSONAPIAdapter.extend({
shouldBackgroundReloadRecord: () => false,
createRecord: (store, type, snapshot) => {
return RSVP.reject();
createRecord: (store, type, snapshot: Snapshot) => {
let count = 0;
snapshot.eachAttribute((attr, attrDef) => {
if (count === 0) {
assert.equal(attr, 'name', 'attribute key is correct');
assert.deepEqual(attrDef, { type: 'string', key: 'name', name: 'name' }, 'attribute def matches schem');
} else if (count === 1) {
assert.equal(attr, 'age', 'attribute key is correct');
assert.deepEqual(attrDef, { type: 'number', key: 'age', name: 'age' }, 'attribute def matches schem');
}
count++;
});
count = 0;
snapshot.eachRelationship((rel, relDef) => {
if (count === 0) {
assert.equal(rel, 'boats', 'relationship key is correct');
assert.deepEqual(
relDef,
{
type: 'ship',
kind: 'hasMany',
inverse: null,
options: {},
key: 'boats',
},
'relationships def matches schem'
);
} else if (count === 1) {
assert.equal(rel, 'house', 'relationship key is correct');
assert.deepEqual(
relDef,
{ type: 'house', kind: 'belongsTo', inverse: null, options: {}, key: 'house', name: 'house' },
'relationship def matches schem'
);
}
count++;
});
return RSVP.resolve({ data: { type: 'person', id: '1' } });
},
})
);
this.owner.register('service:store', CustomStore);
store = this.owner.lookup('service:store');
store.registerSchemaDefinitionService(schemaDefinition);
assert.expect(1);
let schema = {
attributesDefinitonFor(modelName: string) {
assert.equal(modelName, 'person', 'type passed in to the schema hooks');
return {
name: {
type: 'string',
key: 'name',
name: 'name',
},
age: {
type: 'number',
key: 'age',
name: 'age',
},
};
},
relationshipsDefinitionFor(modelName: string) {
assert.equal(modelName, 'person', 'type passed in to the schema hooks');
return {
boats: {
type: 'ship',
kind: 'hasMany',
inverse: null,
options: {},
key: 'boats',
},
house: {
type: 'house',
kind: 'belongsTo',
inverse: null,
options: {},
key: 'house',
name: 'house',
},
};
},
doesTypeExist() {
return true;
},
};
store.registerSchemaDefinitionService(schema);
let person = store.createRecord('person', { name: 'chris' });
person.save();
assert.ok(true);
await person.save();
});

test('hasModelFor with custom schema definition igor9', async function(assert) {
assert.expect(4);
this.owner.register('service:store', CustomStore);
store = this.owner.lookup('service:store');
let count = 0;
let schema = {
attributesDefinitonFor() {
return {};
},
relationshipsDefinitionFor() {
return {};
},
doesTypeExist(modelName: string) {
if (count === 0) {
assert.equal(modelName, 'person', 'type passed in to the schema hooks');
} else if (count === 1) {
assert.equal(modelName, 'boat', 'type passed in to the schema hooks');
}
count++;
return modelName === 'person';
},
};
store.registerSchemaDefinitionService(schema);
assert.equal(store._hasModelFor('person'), true, 'hasModelFor matches schema hook when true');
assert.equal(store._hasModelFor('boat'), false, 'hasModelFor matches schema hook when false');
});

test('record saving', async function(assert) {
assert.expect(1);
this.owner.register(
'adapter:application',
JSONAPIAdapter.extend({
Expand All @@ -188,7 +290,6 @@ if (CUSTOM_MODEL_CLASS) {
);
this.owner.register('service:store', CustomStore);
store = this.owner.lookup('service:store');
assert.expect(1);
let person = store.createRecord('person', { name: 'chris' });
let promisePerson = await store.saveRecord(person);
assert.equal(person, promisePerson, 'save promise resolves with the same record');
Expand Down

0 comments on commit 353cfcd

Please sign in to comment.