Skip to content

Commit

Permalink
Remove DS import in model-relationships tests
Browse files Browse the repository at this point in the history
Instead of the DS.PromiseObject and DS.PromiseArray we now test against a real ember data model.
Both `sync` and `async` behaviors are covered now.
  • Loading branch information
fsmanuel committed Oct 26, 2023
1 parent 0599dff commit 5bc0401
Showing 1 changed file with 90 additions and 16 deletions.
106 changes: 90 additions & 16 deletions tests/integration/validations/model-relationships-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ArrayProxy from '@ember/array/proxy';
import EmberObject from '@ember/object';
import { isNone } from '@ember/utils';
import { A as emberArray } from '@ember/array';
import DS from 'ember-data';
import Model, { belongsTo, hasMany } from '@ember-data/model';
import setupObject from '../../helpers/setup-object';
import DefaultMessages from 'dummy/validators/messages';
import BelongsToValidator from 'ember-cp-validations/validators/belongs-to';
Expand Down Expand Up @@ -508,16 +508,22 @@ module('Integration | Validations | Model Relationships', function (hooks) {
assert.true(user.get('validations.attrs.fullName.isValid'));
});

test('presence on empty DS.PromiseObject', function (assert) {
test('presence on empty belongsTo - sync', function (assert) {
this.owner.register('validator:presence', PresenceValidator);
this.owner.register(
'model:user',
Model.extend(
buildValidations({
friend: validator('presence', true),
}),
{
friend: belongsTo('user', { async: false, inverse: null }),
}
)
);

let Validations = buildValidations({
friend: validator('presence', true),
});

let user = setupObject(this, EmberObject.extend(Validations), {
friend: DS.PromiseObject.create(),
});
const store = this.owner.lookup('service:store');
const user = store.createRecord('user');

let { validations, model } = user.get('validations').validateSync();

Expand All @@ -533,16 +539,53 @@ module('Integration | Validations | Model Relationships', function (hooks) {
assert.strictEqual(friend.get('message'), "This field can't be blank");
});

test('presence on empty DS.PromiseArray', function (assert) {
test('presence on empty belongsTo - async', async function (assert) {
this.owner.register('validator:presence', PresenceValidator);
this.owner.register(
'model:user',
Model.extend(
buildValidations({
friend: validator('presence', true),
}),
{
friend: belongsTo('user', { async: true, inverse: null }),
}
)
);

let Validations = buildValidations({
friends: validator('presence', true),
});
const store = this.owner.lookup('service:store');
const user = store.createRecord('user');

let user = setupObject(this, EmberObject.extend(Validations), {
friends: DS.PromiseArray.create(),
});
let { validations, model } = await user.get('validations').validate();

assert.strictEqual(model, user, 'expected model to be the correct model');
assert.deepEqual(
validations.get('content').getEach('attribute').sort(),
['friend'].sort()
);

let friend = validations.get('content').findBy('attribute', 'friend');

assert.false(friend.get('isValid'));
assert.strictEqual(friend.get('message'), "This field can't be blank");
});

test('presence on empty hasMany - sync', function (assert) {
this.owner.register('validator:presence', PresenceValidator);
this.owner.register(
'model:user',
Model.extend(
buildValidations({
friends: validator('presence', true),
}),
{
friends: hasMany('user', { async: false, inverse: null }),
}
)
);

const store = this.owner.lookup('service:store');
const user = store.createRecord('user');

let { validations, model } = user.get('validations').validateSync();

Expand All @@ -558,6 +601,37 @@ module('Integration | Validations | Model Relationships', function (hooks) {
assert.strictEqual(friends.get('message'), "This field can't be blank");
});

test('presence on empty hasMany - async', async function (assert) {
this.owner.register('validator:presence', PresenceValidator);
this.owner.register(
'model:user',
Model.extend(
buildValidations({
friends: validator('presence', true),
}),
{
friends: hasMany('user', { async: true, inverse: null }),
}
)
);

const store = this.owner.lookup('service:store');
const user = store.createRecord('user');

let { validations, model } = await user.get('validations').validate();

assert.strictEqual(model, user, 'expected model to be the correct model');
assert.deepEqual(
validations.get('content').getEach('attribute').sort(),
['friends'].sort()
);

let friends = validations.get('content').findBy('attribute', 'friends');

assert.false(friends.get('isValid'));
assert.strictEqual(friends.get('message'), "This field can't be blank");
});

test('debounce should work across nested HasMany relationships', function (assert) {
this.owner.register('validator:presence', PresenceValidator);
this.owner.register('validator:has-many', HasManyValidator);
Expand Down

0 comments on commit 5bc0401

Please sign in to comment.