pouchdb-community / ember-pouch

PouchDB/CouchDB adapter for Ember Data
Apache License 2.0
281 stars 76 forks source link

Breaks tests #235

Closed jacobq closed 4 years ago

jacobq commented 6 years ago

Ref #200 #217, #23, #24

I just switched from ember-localstorage-adapter and suddenly lots of tests that were passing now blow up during their tear-down with errors like:

Error: Assertion Failed: You can only unload a record which is not inFlight.

Does this addon not support use in testing? Is there a way to easily band-aid this? (e.g. have all the tests run a hook that does something magic?)

jacobq commented 6 years ago

Also #188

I was able to get past this first problem by installing pouchdb-adapter-memory and using it for the test environment:

// config/environment.js
    // ...
    if (environment === 'test') {
        ENV.emberPouch.options = {
            adapter: 'memory'
        };
        // ...
    }
    // ...
// app/adapters/application.js
import { Adapter } from 'ember-pouch';
import PouchDB from 'pouchdb';
import pouchInMemoryPlugin from 'pouchdb-adapter-memory'; // Note: using ember-auto-import to get this from npm
import config from 'my-app/config/environment';
import { isEmpty } from '@ember/utils';
import { assert } from '@ember/debug';

function createDb() {
  const localDb = config.emberPouch.localDb;

  assert('emberPouch.localDb must be set', !isEmpty(localDb));

  if (config.emberPouch.options && config.emberPouch.options.adapter === "memory") {
      PouchDB.plugin(pouchInMemoryPlugin);
  }
  const db = new PouchDB(localDb, config.emberPouch.options);

  if (config.emberPouch.remoteDb) {
    let remoteDb = new PouchDB(config.emberPouch.remoteDb);

    db.sync(remoteDb, {
      live: true,
      retry: true
    });
  }

  return db;
}

export default Adapter.extend({
  init() {
    this._super(...arguments);
    this.set('db', createDb());
  }
});

However, I now see another problem. Some tests (not the same ones with the previously mentioned problem either) now show this error after they run:

Error: Attempted to handle event becameError on <...> while in state root.loaded.saved.

backspace commented 5 years ago

This inFlight problem has been happening to me too; I’m also finding that a test I have where it saves a model and tries to click a link on the post-save-forwarded index page is failing because it’s unable to find the link. My theory is that the acceptance test is terminating before the route transition, maybe because the promise being returned from Relational Pouch is non-Ember? But I tried wrapping in an RSVP promise to no avail, so my theory isn’t too promising 😐

jacobq commented 5 years ago

@backspace I was able to reproduce the problem by manually using the dev tools console, so there must be more to it than just test waiters/synchronization.

backspace commented 5 years ago

ah, okay. I was able to use the waiter technique discussed in this thread to get rid of the inFlight error but the test still fails for the other reason. If I pauseTest and then manually resumeTest, the test passes 😞

jacobq commented 4 years ago

Closing since this is mostly a duplicate of #237 #239 (once I worked around those the only problems I had were related to my own app code)