Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should I handle a full scenario involving handleCreate and store.query? #143

Closed
jagthedrummer opened this issue Oct 28, 2015 · 18 comments

Comments

@jagthedrummer
Copy link

I'm trying to test a full UI flow that starts with no categories, adds one, and then checks to make sure that it is displayed. I'm running into trouble when I try to use store.query in my route instead of store.findAll.

If my route uses store.findAll, then this acceptance test will work perfectly.

test('create a new category', function(assert) {
  TestHelper.handleFindAll('category', []);
  visit('/admin/categories');

  andThen(function() {
    assert.equal(find('.empty-block').length, 1);
    assert.equal(find('table tbody tr').length, 0);
  });

  click('a:contains(New Category)');

  andThen(function() {
    assert.equal(currentPath(), 'admin.categories.new');

    fillIn('div:contains(Name) input', 'Test Category');
    fillIn('div:contains(Description) input', 'This is only a test!');

    TestHelper.handleCreate('category');
    click('button:submit');
  });

  andThen(function() {
    assert.equal(currentPath(), 'admin.categories.index');
    assert.equal(find('.empty-block').length, 0);
    assert.equal(find('table tbody tr').length, 1);
  });
});

But if I change my route to use store.query and change the first line of the test to be TestHelper.handleQuery('category', {}, []); then the final two assert.equal tests fail, and when I return pauseTest(); before them I can see that as far as Ember is concerned I still don't have any categories.

Am I missing something?

@danielspaniel
Copy link
Collaborator

What version of ed and factory guy are you using?
Can you upgrade to 2.1.0? by any chance?

@jagthedrummer
Copy link
Author

I'm on ED 1.13.14 and EDFG 1.13.10. Does 2.1.0 play nicely with ED 1.13.14?

@danielspaniel
Copy link
Collaborator

it does .. give it a try ( might not fix issue right off ) .. but then I can work from there to find solution

@jagthedrummer
Copy link
Author

Cool! I'll give it a shot and report back. Thanks for the quick replies!

@jagthedrummer
Copy link
Author

OK, I'm seeing the same thing on 2.1.0.

@danielspaniel
Copy link
Collaborator

good ..
did you change
TestHelper.handleFindAll('category', []);
to
TestHelper.handleFindAll('category', 0);

in the test

@danielspaniel
Copy link
Collaborator

without stepping through this I can't quite tell though what is the issue.
Can you see why the handle create is not creating ..
handle create makes a mock url POST '/categories' .. and passes back json for that category .. and I would dig into the mockjax code to see why that handler is not returning
it's not that hard to do ( its in bower_components )
you can also turn on mockjax logging with $.mockjaxSettings.logging = true;

@jagthedrummer
Copy link
Author

It's handleQuery that's giving me problems. Here's the entire test with handleQuery

test('create a new category', function(assert) {
  TestHelper.handleQuery('category', {}, []);

  visit('/admin/categories');

  andThen(function() {
    assert.equal(find('.empty-block').length, 1);
    assert.equal(find('table tbody tr').length, 0);
  });

  click('a:contains(New Category)');

  andThen(function() {
    assert.equal(currentPath(), 'admin.categories.new');

    fillIn('div:contains(Name) input', 'MyString');
    fillIn('div:contains(Description) input', 'MyString');

    TestHelper.handleCreate('category');

    click('button:submit');
  });

  andThen(function() {
    assert.equal(currentPath(), 'admin.categories.index');
    //TODO : Figure out why ember-data-factory-guy doesn't see the new model
    // https://github.com/danielspaniel/ember-data-factory-guy/issues/143
    assert.equal(find('.empty-block').length, 0);
    assert.equal(find('table tbody tr').length, 1);
  });
});

And then my category route is:

export default Ember.Route.extend({
  model: function() {
    return this.store.query('category',{});
  }
});

@danielspaniel
Copy link
Collaborator

I would even be happier if you could make a test like your's that fails on the factory guy repo ( basically write that same test ) so I could fix it much easier

@jagthedrummer
Copy link
Author

To be clear if I use handleFindAll in the test along with store.findAll in the route it works. If I use handleQuery along with store.query in the route it doesn't work.

@danielspaniel
Copy link
Collaborator

its odd because handleQuery mocks a GET request .. so this is highly interesting case of huh?
and yes, I get you .. I would snoop like I said at the mockjax code to see what happens when that category is created ( when you click('button:submit'); )

jagthedrummer added a commit to jagthedrummer/ember-data-factory-guy that referenced this issue Oct 28, 2015
…pted-ember-addons#143)

This is just a quick and dirty import of the category acceptance test
from my project. I pulled in everything that I needed to make the other
parts of it pass, just to sanity check that things were working.
@jagthedrummer
Copy link
Author

OK, here's a PR with a test that's failing the same way : #144

@danielspaniel
Copy link
Collaborator

super .. thanks .. I will snoop around and let you know

@danielspaniel
Copy link
Collaborator

in that test:

test('create a new category', function(assert) {
  //TestHelper.handleQuery('category', {}, []);
  TestHelper.handleFindAll('category', 0);
  visit('/categories');

it is failing even with TestHelper.handleFindAll('category', 0);
do you get that also?

@danielspaniel
Copy link
Collaborator

never mind .. its the index route
return this.store.findAll('category',{});
that was changed ..

@jagthedrummer
Copy link
Author

Argh! I was testing different things and accidentally committed the wrong one. I'll update the PR so that it's at least consistent with what I'm trying to isolate.

@danielspaniel
Copy link
Collaborator

looks like the answer might be here:

Note: When creating a new record using any of the above methods Ember Data will update DS.RecordArrays such as those returned by store#peekAll(), store#findAll() or store#filter().

with store.query() .. its going to make new array every time ..
and since the handleQuery returns nothing since at the beginning of the test you have

TestHelper.handleQuery('category', {});
 visit('/categories');

and to fix it you would have to be able to do this:

   TestHelper.handleCreate('category');
    TestHelper.handleQuery('category', {}, [{id:1}]);  // basically say .. hey .. I just made that category ( it's id is 1 ( most likely ) .. so pass that one back ) 

    click('button:submit');

and since that ability is not available

  TestHelper.handleQuery('category', {}, [{id:1}]);

you can't do anything.

But luckily .. this very issue has a solution proposed by someone else . .and I am going to implement it asap .. like in the next 4 days.

see issue #139

so you would do this:

handleQuery('category', {)).returnsJSON([{id: 1}]);

@danielspaniel
Copy link
Collaborator

This issue is now fixed in the newest release 2.1.1

I made a test just for you:

  // test created for issue #143
  test("query for none than create then query again", function (assert) {
    Ember.run(function () {
      var done = assert.async();
      var store = FactoryGuy.getStore();

      var bobQueryHander = TestHelper.handleQuery('user', {name: 'Bob'});

      store.query('user', {name: 'Bob'}).then(function (users) {
        equal(users.get('length'), 0);

        TestHelper.handleCreate('user', {name: 'Bob'});
        store.createRecord('user', {name: 'Bob'}).save().then(function(user){

          bobQueryHander.returnsExistingIds([1]);

          store.query('user', {name: 'Bob'}).then(function (users) {
            equal(users.get('length'), 1);
            done();
          });
        });
      });
    });
  });

If you don't get it, let me know and I will explain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants