return run(async () => {
        let json1 = build('user'),
            json2 = build('user'),
            mock1 = mockQueryRecord('user', {id: 1}).returns({json: json1});

        mockQueryRecord('user', {}).returns({json: json2});

        assert.notOk(mock1.isDestroyed, "isDestroyed is false initially");

        let data = await FactoryGuy.store.queryRecord('user', {id: 1});
        assert.equal(data.get('id'), json1.get('id'), "the first mock works initially");

        mock1.disable();
        data = await FactoryGuy.store.queryRecord('user', {id: 1});
        assert.equal(data.get('id'), json2.get('id'), "the first mock doesn't work once it's disabled");

        mock1.enable();
        data = await FactoryGuy.store.queryRecord('user', {id: 1});
        assert.equal(data.get('id'), json1.get('id'), "the first mock works again after enabling");

        mock1.destroy();
        assert.ok(mock1.isDestroyed, "isDestroyed is set to true once the mock is destroyed");
        data = await FactoryGuy.store.queryRecord('user', {id: 1});
        assert.equal(data.get('id'), json2.get('id'), "the destroyed first mock doesn't work");
      });
      return run(async () => {
        const mock = mockFindAll('company');

        await FactoryGuy.store.findAll('company');
        await FactoryGuy.store.findAll('company');
        assert.equal(mock.timesCalled, 2);
      });
    Ember.run(function() {
      let done = assert.async();
      let expectedAssertions = 2;

      function finalizeTest() {
        --expectedAssertions;
        if (expectedAssertions === 0) {
          done();
        }
      }

      let companies = makeList('company', 2);

      mockQuery('company', { name: 'Dude' }).returns({ models: companies });
      mockQuery('company', { type: 'Small', name: 'Dude' }).returns({ models: companies });

      let request1 = FactoryGuy.store.query('company', { name: 'Dude' });
      let request2 = FactoryGuy.store.query('company', { type: 'Small', name: 'Dude' });

      request1.then(function(returnedCompanies) {
        equal(companies.mapBy('id') + '', returnedCompanies.mapBy('id') + '');
        finalizeTest();
      });

      request2.then(function(returnedCompanies) {
        equal(companies.mapBy('id') + '', returnedCompanies.mapBy('id') + '');
        finalizeTest();
      });
    });
      return run(async () => {
        const mock = mockQueryRecord('company', {}).returns({json: build('company')});

        await FactoryGuy.store.queryRecord('company', {});
        await FactoryGuy.store.queryRecord('company', {});
        assert.equal(mock.timesCalled, 2);
      });
 FactoryGuy.store.queryRecord('user', { name: 'Bob' }).then(function(user) {
   equal(user.id, bob.get('id'));
   equal(user.get('name'), bob.get('name'));
   // makes the user after getting query response
   equal(FactoryGuy.store.peekAll('user').get('content').length, 1);
   done();
 });
Esempio n. 6
0
test("exposes makeList method which is shortcut for FactoryGuy.makeList", function () {
  let users = makeList('user', 2);
  equal(users.length, 2);
  ok(users[0] instanceof User);
  ok(users[1] instanceof User);
  equal(FactoryGuy.store.peekAll('user').get('content').length, 2);
});
 FactoryGuy.store.query('user', { name: 'Bob' }).then(function(users) {
   equal(users.get('length'), 1);
   equal(users.get('firstObject'), bob);
   // does not create a new model
   equal(FactoryGuy.store.peekAll('user').get('content').length, 1);
   done();
 });
  test("creates records in the store", function () {
    let user = make('user');
    ok(user instanceof User);

    let storeUser = FactoryGuy.store.peekRecord('user', user.id);
    ok(storeUser === user);
  });
 test("creates list of DS.Model instances", function () {
   let users = FactoryGuy.makeList('user', 2);
   equal(users.length, 2);
   ok(users[0] instanceof User);
   ok(users[1] instanceof User);
   equal(FactoryGuy.store.peekAll('user').get('content').length, 2);
 });
test("using custom serialize keys function for transforming attributes and relationship keys", function() {
  let serializer = FactoryGuy.store.serializerFor('application');

  let savedKeyForAttributeFn = serializer.keyForAttribute;
  serializer.keyForAttribute = Ember.String.dasherize;
  let savedKeyForRelationshipFn = serializer.keyForRelationship;
  serializer.keyForRelationship = Ember.String.dasherize;

  let buildJson = build('profile', 'with_bat_man');
  buildJson.unwrap();

  let expectedJson = {
    profile: {
      id: 1,
      description: 'Text goes here',
      'camel-case-description': 'textGoesHere',
      'snake-case-description': 'text_goes_here',
      'a-boolean-field': false,
      'super-hero': 1,
    },
    'super-heros': [
      {
        id: 1,
        name: "BatMan",
        type: "SuperHero"
      }
    ]
  };

  deepEqual(buildJson, expectedJson);

  serializer.keyForAttribute = savedKeyForAttributeFn;
  serializer.keyForRelationship = savedKeyForRelationshipFn;
});
      FactoryGuy.store.query('company', { name: 'Dude' }).then(function(companies) {
        equal(companies.mapBy('id') + '', companies1.mapBy('id') + '');

        FactoryGuy.store.query('company', { type: 'Small' }).then(function(companies) {
          equal(companies.mapBy('id') + '', companies2.mapBy('id') + '');
          done();
        });
      });
 Ember.run(function() {
   let done = assert.async();
   mockQuery('user', { name: 'Bob' }).returns({ models: [] });
   FactoryGuy.store.query('user', { name: 'Bob' }).then(function(users) {
     equal(users.get('length'), 0, "nothing returned");
     done();
   });
 });
    Ember.run(function() {
      let done = assert.async();
      let customDescription = "special description";

      mockCreate('profile', {
        match: { description: customDescription }
      });
      ok(FactoryGuy.store.peekAll('profile').get('content.length') === 0);
      FactoryGuy.store.createRecord('profile', {
        description: customDescription
      }).save().then(function(profile) {
        ok(FactoryGuy.store.peekAll('profile').get('content.length') === 1, 'No extra records created');
        ok(profile instanceof Profile, 'Creates the correct type of record');
        ok(profile.get('description') === customDescription, 'Passes along the match attributes');
        done();
      });
    });
 FactoryGuy.store.query('user', { name: 'Bob' }).then(function(users) {
   equal(users.get('length'), 2);
   equal(users.get('firstObject.name'), 'User1');
   equal(users.get('firstObject.hats.length'), 2);
   equal(users.get('lastObject.name'), 'User2');
   equal(FactoryGuy.store.peekAll('user').get('content.length'), 2, 'no new instances created');
   done();
 });
    Ember.run(function() {
      let done = assert.async();
      let customName = "special name";

      mockCreate('super-hero', {
        match: { name: customName }
      });
      ok(FactoryGuy.store.peekAll('super-hero').get('content.length') === 0);
      FactoryGuy.store.createRecord('super-hero', {
        name: customName
      }).save().then(function(superHero) {
        ok(FactoryGuy.store.peekAll('super-hero').get('content.length') === 1, 'No extra records created');
        ok(superHero instanceof SuperHero, 'Creates the correct type of record');
        ok(superHero.get('name') === customName, 'Passes along the match attributes');
        done();
      });
    });
      FactoryGuy.store.queryRecord('company', { name: 'Dude' }).then(function(company) {
        equal(company.get('id'), company1.get('id'));

        FactoryGuy.store.queryRecord('company', { type: 'Small' }).then(function(company) {
          equal(company.get('id'), company2.get('id'));
          done();
        });
      });
    Ember.run(function() {
      let done = assert.async();

      let models = makeList('user', 2, 'with_hats');
      mockQuery('user', { name: 'Bob' }).returns({ models });

      equal(FactoryGuy.store.peekAll('user').get('content.length'), 2, 'start out with 2 instances');

      FactoryGuy.store.query('user', { name: 'Bob' }).then(function(users) {
        equal(users.get('length'), 2);
        equal(users.get('firstObject.name'), 'User1');
        equal(users.get('firstObject.hats.length'), 2);
        equal(users.get('lastObject.name'), 'User2');
        equal(FactoryGuy.store.peekAll('user').get('content.length'), 2, 'no new instances created');
        done();
      });
    });
test("with namespace and host", function() {
  let adapter = FactoryGuy.store.adapterFor('application');
  adapter.setProperties({
    host: 'https://dude.com',
    namespace: 'api/v1'
  });

  equal(FactoryGuy.buildURL('project'), 'https://dude.com/api/v1/projects');
});
      FactoryGuy.store.queryRecord('company', { name: 'Dude' }).then(function(company) {
        equal(company.get('id'), company1.get('id'));

        mockQuery.withParams({ type: 'Small' }).returns({ json: company2 });
        FactoryGuy.store.queryRecord('company', { type: 'Small' }).then(function(company) {
          equal(company.get('id'), company2.get('id'));
          done();
        });
      });
    Ember.run(function() {
      let done = assert.async();
      mockFindAll('user', 0);

      FactoryGuy.store.findAll('user').then(function(profiles) {
        ok(profiles.get('length') === 0);
        done();
      });
    });
 test("with no parameters matches queryRequest with any parameters", function(assert) {
   var done = assert.async();
   mockQueryRecord('user');
   FactoryGuy.store.queryRecord('user', { name: 'Bob' })
     .then(()=> {
       ok(true);
       done();
     });
 });
 FactoryGuy.store.queryRecord('user', { id: 1 }).then((data)=> {
   assert.equal(data.get('id'), json1.get('id'), "the first mock works again after enabling");
   mock1.destroy();
   assert.ok(mock1.isDestroyed, "isDestroyed is set to true once the mock is destroyed");
   FactoryGuy.store.queryRecord('user', { id: 1 }).then((data)=> {
     assert.equal(data.get('id'), json2.get('id'), "the destroyed first mock doesn't work");
     done();
   });
 });
      FactoryGuy.store.query('company', { name: 'Dude' }).then(function(companies) {
        equal(companies.mapBy('id') + '', companies1.mapBy('id') + '');

        queryHandler.withParams({ type: 'Small' }).returns({ models: companies2 });
        FactoryGuy.store.query('company', { type: 'Small' }).then(function(companies) {
          equal(companies.mapBy('id') + '', companies2.mapBy('id') + '');
          done();
        });
      });
    Ember.run(function() {
      let done = assert.async();

      let models = makeList('company', 2, 'with_projects', 'with_profile');
      mockQuery('company', { name: 'Dude Company' }).returns({ models });

      equal(FactoryGuy.store.peekAll('company').get('content.length'), 2, 'start out with 2 instances');

      FactoryGuy.store.query('company', { name: 'Dude Company' }).then(function(companies) {
        equal(companies.get('length'), 2);
        ok(companies.get('firstObject.profile') instanceof Profile);
        equal(companies.get('firstObject.projects.length'), 2);
        ok(companies.get('lastObject.profile') instanceof Profile);
        equal(companies.get('lastObject.projects.length'), 2);
        equal(FactoryGuy.store.peekAll('company').get('content.length'), 2, 'no new instances created');
        done();
      });
    });
Esempio n. 25
0
test("without a number but with options returns array of models", function () {
  let profiles = makeList('profile', 'goofy_description', ['with_company', {description: 'Noodles'}], 'with_bat_man');
  equal(profiles.length, 3);
  ok(A(profiles).objectAt(0).get('description') === 'goofy');
  ok(A(profiles).objectAt(1).get('company.name') === 'Silly corp');
  ok(A(profiles).objectAt(1).get('description') === 'Noodles');
  ok(A(profiles).objectAt(2).get('superHero.name') === 'BatMan');
  equal(FactoryGuy.store.peekAll('profile').get('content').length, 3);
});
 FactoryGuy.store.query('company', { name: 'Dude Company' }).then(function(companies) {
   equal(companies.get('length'), 2);
   ok(companies.get('firstObject.profile') instanceof Profile);
   equal(companies.get('firstObject.projects.length'), 2);
   ok(companies.get('lastObject.profile') instanceof Profile);
   equal(companies.get('lastObject.projects.length'), 2);
   equal(FactoryGuy.store.peekAll('company').get('content.length'), 2, 'no new instances created');
   done();
 });
test("#getUrl uses urlForDeleteRecord if it is set on the adapter", function(assert) {
  let mock1 = mockDelete('user', '2');
  assert.equal(mock1.getUrl(), '/users/2');

  let adapter = FactoryGuy.store.adapterFor('user');
  sinon.stub(adapter, 'urlForDeleteRecord').returns('/deleteMyZombie/2');

  assert.equal(mock1.getUrl(), '/deleteMyZombie/2');
  adapter.urlForDeleteRecord.restore();
});
test("#getUrl uses urlForCreateRecord if it is set on the adapter", function(assert) {
  let mock1 = mockCreate('user');
  assert.equal(mock1.getUrl(), '/users');

  let adapter = FactoryGuy.store.adapterFor('user');
  sinon.stub(adapter, 'urlForCreateRecord').returns('/makeMeAZombie');

  assert.equal(mock1.getUrl(), '/makeMeAZombie');
  adapter.urlForCreateRecord.restore();
});
test("#getUrl uses urlForFindAll if it is set on the adapter", function(assert) {
  let mock1 = mockFindAll('user');
  assert.equal(mock1.getUrl(), '/users');

  let adapter = FactoryGuy.store.adapterFor('user');
  sinon.stub(adapter, 'urlForFindAll').returns('/zombies');

  assert.equal(mock1.getUrl(), '/zombies');
  adapter.urlForFindAll.restore();
});
test("#getUrl uses urlForFindRecord if it is set on the adapter", function(assert) {
  let mock1 = mockFindRecord('user');
  assert.equal(mock1.getUrl(), '/users/1');

  let adapter = FactoryGuy.store.adapterFor('user');
  sinon.stub(adapter, 'urlForFindRecord').returns('/dude/1');

  assert.equal(mock1.getUrl(), '/dude/1');
  adapter.urlForFindRecord.restore();
});