Skip to content

Modular logistics and supply chain operating system (LSOS)

test(console): cover the console controller's reload timers and menu defaults

Ronald A. Richardson30349847af81
diff --git a/console/app/controllers/console.js b/console/app/controllers/console.js
index 0671cd5d..92504692 100644
--- a/console/app/controllers/console.js
+++ b/console/app/controllers/console.js
@@ -86,7 +86,7 @@ export default class ConsoleController extends Controller {
return later(
this,
() => {
- window.location.reload();
+ this.reloadWindow();
},
900
);
@@ -110,7 +110,7 @@ export default class ConsoleController extends Controller {
return later(
this,
() => {
- window.location.reload();
+ this.reloadWindow();
},
900
);
@@ -147,7 +147,7 @@ export default class ConsoleController extends Controller {
return later(
this,
() => {
- window.location.reload();
+ this.reloadWindow();
},
900
);
@@ -159,6 +159,15 @@ export default class ConsoleController extends Controller {
});
}
+ /* istanbul ignore next -- window.location.reload() cannot be stubbed: the HTML spec
+ makes every Location member an own, non-configurable property, so defineProperty
+ throws and calling it for real would reload the test runner. Extracted so the reload
+ timers above can be tested by replacing this on the instance, mirroring
+ InstallationService.reloadConsole(). */
+ reloadWindow() {
+ window.location.reload();
+ }
+
@action viewChangelog() {
this.modalsManager.show('modals/changelog', {
title: this.intl.t('common.changelog'),
diff --git a/console/tests/unit/controllers/console-test.js b/console/tests/unit/controllers/console-test.js
index 18c1cb9f..497097ef 100644
--- a/console/tests/unit/controllers/console-test.js
+++ b/console/tests/unit/controllers/console-test.js
@@ -1,5 +1,6 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
+import { settled } from '@ember/test-helpers';
import Service from '@ember/service';
import { cancel } from '@ember/runloop';
@@ -252,3 +253,109 @@ module('Unit | Controller | console', function (hooks) {
assert.false(modal.loading);
});
});
+
+module('Unit | Controller | console | reload timers and defaults', function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.postRejectsWith = null;
+ const context = this;
+
+ class FetchStub extends Service {
+ post() {
+ return context.postRejectsWith ? Promise.reject(context.postRejectsWith) : Promise.resolve({ ok: true });
+ }
+ flushRequestCache() {}
+ }
+ class NotificationsStub extends Service {
+ success() {}
+ serverError() {}
+ }
+ class ModalsManagerStub extends Service {
+ shown = [];
+ confirmed = [];
+ options = {};
+ show(name, options) {
+ this.shown.push({ name, options });
+ this.options = options;
+ }
+ confirm(options) {
+ this.confirmed.push(options);
+ this.options = options;
+ }
+ getOptions() {
+ return this.options;
+ }
+ setOption(key, value) {
+ this.options[key] = value;
+ }
+ }
+ this.owner.register('service:fetch', FetchStub);
+ this.owner.register('service:notifications', NotificationsStub);
+ this.owner.register('service:modals-manager', ModalsManagerStub);
+
+ this.build = () => {
+ const controller = this.owner.lookup('controller:console');
+ this.reloads = 0;
+ Object.defineProperty(controller, 'reloadWindow', { configurable: true, value: () => this.reloads++ });
+ return controller;
+ };
+ this.modals = () => this.owner.lookup('service:modals-manager');
+ this.modal = () => ({ startLoading() {}, stopLoading() {}, getOptions: () => this.modals().options });
+ });
+
+ test('the menus and organization list start empty', function (assert) {
+ const controller = this.build();
+
+ // Read before anything writes them: a @tracked initializer only runs on first access.
+ assert.deepEqual(controller.organizations, []);
+ assert.deepEqual(controller.menuItems, []);
+ assert.deepEqual(controller.userMenuItems, []);
+ assert.deepEqual(controller.organizationMenuItems, []);
+ });
+
+ test('joining an organization reloads the console once the timer fires', async function (assert) {
+ const controller = this.build();
+ controller.createOrJoinOrg();
+ this.modals().options.action = 'join';
+
+ await this.modals().options.confirm(this.modal());
+ assert.strictEqual(this.reloads, 0, 'the reload is deferred, not immediate');
+
+ await settled();
+
+ assert.strictEqual(this.reloads, 1, 'the console reloads into the joined organization');
+ });
+
+ test('creating an organization reloads the console once the timer fires', async function (assert) {
+ const controller = this.build();
+ controller.createOrJoinOrg();
+ this.modals().options.action = 'create';
+
+ await this.modals().options.confirm(this.modal());
+ await settled();
+
+ assert.strictEqual(this.reloads, 1);
+ });
+
+ test('switching organization reloads the console once the timer fires', async function (assert) {
+ const controller = this.build();
+ controller.switchOrganization({ uuid: 'co_2', name: 'Other Co' });
+
+ await this.modals().options.confirm(this.modal());
+ await settled();
+
+ assert.strictEqual(this.reloads, 1);
+ });
+
+ test('a failed switch never reaches the reload', async function (assert) {
+ this.postRejectsWith = new Error('switch rejected');
+ const controller = this.build();
+ controller.switchOrganization({ uuid: 'co_2', name: 'Other Co' });
+
+ await this.modals().options.confirm(this.modal());
+ await settled();
+
+ assert.strictEqual(this.reloads, 0);
+ });
+});