Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
run(() => {
let done = assert.async();
let json = buildList('user', 1);
mockQuery('user', {name: 'Bob'}).returns({json});
FactoryGuy.store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 1);
// makes the user after getting query response
assert.equal(FactoryGuy.store.peekAll('user').get('content').length, 1);
done();
});
});
});
test("using returns with headers adds the headers to the response", function(assert) {
var done = assert.async();
const queryParams = {name: 'MyCompany'};
const handler = mockQuery('company', queryParams);
handler.returns({headers: {'X-Testing': 'absolutely'}});
$(document).ajaxComplete(function(event, xhr) {
assert.equal(xhr.getResponseHeader('X-Testing'), 'absolutely');
$(document).off('ajaxComplete');
done();
});
FactoryGuy.store.query('company', queryParams);
});
test("using returns with model instances having hasMany models", async function(assert) {
let models = makeList('user', 2, 'with_hats');
mockQuery('user', {name: 'Bob'}).returns({models});
assert.equal(FactoryGuy.store.peekAll('user').get('content.length'), 2, 'start out with 2 instances');
let users = await Ember.run(async () => FactoryGuy.store.query('user', {name: 'Bob'}));
assert.equal(users.get('length'), 2);
assert.equal(users.get('firstObject.name'), 'User1');
assert.equal(users.get('firstObject.hats.length'), 2);
assert.equal(users.get('lastObject.name'), 'User2');
assert.equal(FactoryGuy.store.peekAll('user').get('content.length'), 2, 'no new instances created');
});
test("using returns with headers adds the headers to the response", function(assert) {
var done = assert.async();
const queryParams = {name: 'MyCompany'};
const handler = mockQuery('company', queryParams);
handler.returns({headers: {'X-Testing': 'absolutely'}});
$(document).ajaxComplete(function(event, xhr) {
assert.equal(xhr.getResponseHeader('X-Testing'), 'absolutely');
$(document).off('ajaxComplete');
done();
});
FactoryGuy.store.query('company', queryParams);
});
run(() => {
let done = assert.async();
let companies1 = makeList('company', 2);
let companies2 = makeList('company', 2);
let matchQueryHandler = mockQuery('company').withSomeParams({name: 'Dude'}).returns({models: companies1});
let allQueryHandler = mockQuery('company').returns({models: companies2});
FactoryGuy.store.query('company', {name: 'Dude', page: 1}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies1).mapBy('id') + '');
assert.equal(matchQueryHandler.timesCalled, 1);
FactoryGuy.store.query('company', {name: 'Other', page: 1}).then(function(companies) {
assert.equal(A(companies).mapBy('id') + '', A(companies2).mapBy('id') + '');
assert.equal(allQueryHandler.timesCalled, 1);
done();
});
});
});
});
run(() => {
let done = assert.async();
let json1 = buildList('profile', 2).add({meta: {previous: '/profiles?page=1', next: '/profiles?page=3'}});
let json2 = buildList('profile', 2).add({meta: {previous: '/profiles?page=2', next: '/profiles?page=4'}});
mockQuery('profile', {page: 2}).returns({json: json1});
mockQuery('profile', {page: 3}).returns({json: json2});
FactoryGuy.store.query('profile', {page: 2}).then(function(profiles) {
assert.deepEqual(profiles.mapBy('id'), ["1", "2"]);
assert.ok(isEquivalent(profiles.get('meta'), {previous: '/profiles?page=1', next: '/profiles?page=3'}));
FactoryGuy.store.query('profile', {page: 3}).then(function(profiles2) {
assert.deepEqual(profiles2.mapBy('id'), ["3", "4"]);
assert.ok(isEquivalent(profiles2.get('meta'), {previous: '/profiles?page=2', next: '/profiles?page=4'}));
done();
});
});
});
});
run(() => {
let done = assert.async();
let errors = {errors: {name: ['wrong']}};
let mock = mockQuery('user').fails({status: 422, response: errors});
FactoryGuy.store.query('user', {})
.catch(() => {
assert.equal(mock.timesCalled, 1);
assert.ok(true);
done();
});
});
});
run(() => {
let done = assert.async();
let store = FactoryGuy.store;
let bobQueryHander = mockQuery('user', {name: 'Bob'});
store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 0);
mockCreate('user', {name: 'Bob'});
store.createRecord('user', {name: 'Bob'}).save().then(function(user) {
bobQueryHander.returns({models: [user]});
store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 1);
done();
});
});
});
});
run(() => {
let done = assert.async();
mockQuery('user', {name: 'Bob'});
FactoryGuy.store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 0, "nothing returned");
done();
});
});
});
run(() => {
let done = assert.async();
let store = FactoryGuy.store;
let bobQueryHander = mockQuery('user', {name: 'Bob'});
store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 0);
mockCreate('user', {name: 'Bob'});
store.createRecord('user', {name: 'Bob'}).save().then(function(user) {
bobQueryHander.returns({models: [user]});
store.query('user', {name: 'Bob'}).then(function(users) {
assert.equal(users.get('length'), 1);
done();
});
});
});
});