Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
test(console): isolate ember-local-storage buckets for every test
Ronald A. Richardsonf21cafe307e4
diff --git a/console/tests/helpers/index.js b/console/tests/helpers/index.jsindex 83a7e5cb..5add2c00 100644--- a/console/tests/helpers/index.js+++ b/console/tests/helpers/index.js@@ -1,11 +1,33 @@import { setupApplicationTest as upstreamSetupApplicationTest, setupRenderingTest as upstreamSetupRenderingTest, setupTest as upstreamSetupTest } from 'ember-qunit';+import { _resetStorages } from 'ember-local-storage/helpers/storage';// This file exists to provide wrappers around ember-qunit's// test setup functions. This way, you can easily extend the setup that is// needed per test type.+/**+ * ember-local-storage's storageFor() memoizes each bucket in a module-level cache, so a+ * bucket is built inside whichever test container touches it first and is then reused by+ * every later container — outliving its owner. Several core services read through it+ * (current-user, app-cache, fetch), so without this reset state leaks between tests and+ * touching the stale object throws "Cannot create a new tag ... after it has been+ * destroyed" — a global error that aborts the whole run.+ */+function setupLocalStorageIsolation(hooks) {+ hooks.beforeEach(function () {+ _resetStorages();+ window.localStorage.clear();+ });++ hooks.afterEach(function () {+ _resetStorages();+ window.localStorage.clear();+ });+}+function setupApplicationTest(hooks, options) {upstreamSetupApplicationTest(hooks, options);+ setupLocalStorageIsolation(hooks);// Additional setup for application tests can be done here.//@@ -25,12 +47,14 @@ function setupApplicationTest(hooks, options) {function setupRenderingTest(hooks, options) {upstreamSetupRenderingTest(hooks, options);+ setupLocalStorageIsolation(hooks);// Additional setup for rendering tests can be done here.}function setupTest(hooks, options) {upstreamSetupTest(hooks, options);+ setupLocalStorageIsolation(hooks);// Additional setup for unit tests can be done here.}diff --git a/console/tests/unit/controllers/console/account/organizations-test.js b/console/tests/unit/controllers/console/account/organizations-test.jsindex 208b8943..7cd90779 100644--- a/console/tests/unit/controllers/console/account/organizations-test.js+++ b/console/tests/unit/controllers/console/account/organizations-test.js@@ -110,6 +110,22 @@ module('Unit | Controller | console/account/organizations', function (hooks) {this.owner.register('service:notifications', NotificationsStub);this.owner.register('service:intl', IntlStub);+ // Registered before the controller is looked up — the controller resolves its+ // injections on creation, so a stub registered later would never reach it and the+ // real fetch service would issue a request.+ this.posted = [];+ const posted = this.posted;+ class FetchStub extends Service {+ post(path, payload) {+ posted.push({ path, payload });+ return this.postResponse ? this.postResponse(path, payload) : Promise.resolve({});+ }+ flushRequestCache(path) {+ posted.push({ flushed: path });+ }+ }+ this.owner.register('service:fetch', FetchStub);+this.controller = this.owner.lookup('controller:console/account/organizations');this.modals = this.owner.lookup('service:modals-manager');this.notifications = this.owner.lookup('service:notifications');@@ -237,22 +253,7 @@ module('Unit | Controller | console/account/organizations', function (hooks) {});test('createOrganization posts the new organization and refreshes', async function (assert) {- const posted = [];- class FetchStub extends Service {- post(path, payload) {- posted.push({ path, payload });- return Promise.resolve({});- }- flushRequestCache(path) {- posted.push({ flushed: path });- }- }- this.owner.register('service:fetch', FetchStub);-- const controller = this.owner.lookup('controller:console/account/organizations');- controller.router.refresh = () => this.refreshed++;-- controller.createOrganization();+ this.controller.createOrganization();const options = this.modals.lastOptions;options.organization.name = 'New Co';@@ -260,23 +261,16 @@ module('Unit | Controller | console/account/organizations', function (hooks) {// this confirm handler reads the edited organization back via modal.getOption()await options.confirm(fakeModal(options));- assert.strictEqual(posted[0].path, 'auth/create-organization');- assert.strictEqual(posted[0].payload.name, 'New Co');- assert.deepEqual(posted[1], { flushed: 'auth/organizations' });+ assert.strictEqual(this.posted[0].path, 'auth/create-organization');+ assert.strictEqual(this.posted[0].payload.name, 'New Co');+ assert.deepEqual(this.posted[1], { flushed: 'auth/organizations' });assert.strictEqual(this.refreshed, 1);});test('switchOrganization reports a server error without reloading', async function (assert) {- class FetchStub extends Service {- post() {- return Promise.reject(new Error('nope'));- }- flushRequestCache() {}- }- this.owner.register('service:fetch', FetchStub);+ this.controller.fetch.postResponse = () => Promise.reject(new Error('nope'));- const controller = this.owner.lookup('controller:console/account/organizations');- controller.switchOrganization(organization());+ this.controller.switchOrganization(organization());const modal = fakeModal();await this.modals.lastOptions.confirm(modal);@@ -286,20 +280,8 @@ module('Unit | Controller | console/account/organizations', function (hooks) {});test('switchOrganization posts the switch and schedules a reload on success', async function (assert) {- const posted = [];- class FetchStub extends Service {- post(path, payload) {- posted.push({ path, payload });- return Promise.resolve({});- }- flushRequestCache(path) {- posted.push({ flushed: path });- }- }- this.owner.register('service:fetch', FetchStub);-- const controller = this.owner.lookup('controller:console/account/organizations');- controller.switchOrganization(organization());+ const posted = this.posted;+ this.controller.switchOrganization(organization());// The success path returns the `later` timer that reloads the page — cancel it// so the pending reload never fires against the test runner.