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): cover the settings index and the controllers' default state
Ronald A. Richardsond02badf142ec
diff --git a/console/tests/unit/controllers/console/settings/index-test.js b/console/tests/unit/controllers/console/settings/index-test.jsindex 06c43afe..a408c2db 100644--- a/console/tests/unit/controllers/console/settings/index-test.js+++ b/console/tests/unit/controllers/console/settings/index-test.js@@ -46,3 +46,108 @@ module('Unit | Controller | console/settings/index', function (hooks) {assert.true(this.owner.lookup('service:notifications').succeeded);});});++module('Unit | Controller | console/settings/index | saving and uploads', function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ this.uploads = [];+ this.timezones = ['UTC', 'America/New_York'];+ this.getRejectsWith = null;+ this.saveRejectsWith = null;+ const context = this;++ class FetchStub extends Service {+ get() {+ return context.getRejectsWith ? Promise.reject(context.getRejectsWith) : Promise.resolve(context.timezones);+ }+ uploadFile = {+ perform: (file, meta, onUploaded) => {+ context.uploads.push({ file, meta });+ return onUploaded({ id: 'file_1', url: 'https://cdn.example.com/logo.png' });+ },+ };+ }+ class NotificationsStub extends Service {+ successes = [];+ success(message) {+ this.successes.push(message);+ }+ serverError() {}+ }+ this.owner.register('service:fetch', FetchStub);+ this.owner.register('service:notifications', NotificationsStub);++ this.saved = 0;+ this.build = async () => {+ const controller = this.owner.lookup('controller:console/settings/index');+ controller.model = {+ setProperties(properties) {+ Object.assign(this, properties);+ },+ save: () => {+ context.saved++;+ return context.saveRejectsWith ? Promise.reject(context.saveRejectsWith) : Promise.resolve();+ },+ };+ await controller.loadTimezones.last;+ return controller;+ };+ this.notifications = () => this.owner.lookup('service:notifications');+ });++ test('it starts with empty upload queues and loads the timezone list', async function (assert) {+ const controller = await this.build();++ assert.deepEqual(controller.uploadQueue, []);+ assert.deepEqual(controller.uploadedFiles, []);+ assert.deepEqual(controller.timezones, this.timezones);+ });++ test('a failed timezone load leaves the list empty without interrupting the user', async function (assert) {+ this.getRejectsWith = new Error('lookup unavailable');+ const controller = await this.build();++ assert.deepEqual(controller.timezones, []);+ });++ test('saveSettings persists the organization and reports success', async function (assert) {+ const controller = await this.build();++ await controller.saveSettings.perform();++ assert.strictEqual(this.saved, 1);+ assert.deepEqual(this.notifications().successes, ['Organization changes successfully saved.']);+ });++ test('saveSettings prevents a form submission from navigating', async function (assert) {+ const controller = await this.build();+ const event = new Event('submit', { cancelable: true });++ await controller.saveSettings.perform(event);++ assert.true(event.defaultPrevented);+ });++ test('a failed save is swallowed rather than shown as a success', async function (assert) {+ this.saveRejectsWith = new Error('save rejected');+ const controller = await this.build();++ await controller.saveSettings.perform();++ assert.deepEqual(this.notifications().successes, [], 'nothing is reported as saved');+ });++ test('uploadFile files the asset under the company and records it on the model', async function (assert) {+ const controller = await this.build();++ await controller.uploadFile('logo', { name: 'logo.png' });++ assert.strictEqual(this.uploads[0].meta.key_type, 'company');+ assert.strictEqual(this.uploads[0].meta.type, 'logo');+ assert.ok(this.uploads[0].meta.path.endsWith('/logo'), `the asset type is part of the path (got ${this.uploads[0].meta.path})`);+ assert.strictEqual(controller.model.logo_uuid, 'file_1');+ assert.strictEqual(controller.model.logo_url, 'https://cdn.example.com/logo.png');+ assert.deepEqual(controller.model.logo, { id: 'file_1', url: 'https://cdn.example.com/logo.png' });+ });+});diff --git a/console/tests/unit/controllers/onboard/verify-email-test.js b/console/tests/unit/controllers/onboard/verify-email-test.jsindex 39cc44bf..3d4935db 100644--- a/console/tests/unit/controllers/onboard/verify-email-test.js+++ b/console/tests/unit/controllers/onboard/verify-email-test.js@@ -68,3 +68,35 @@ module('Unit | Controller | onboard/verify-email', function (hooks) {assert.strictEqual(transitioned, 'auth.login');});});++module('Unit | Controller | onboard/verify-email | failure', function (hooks) {+ setupTest(hooks);++ test('a failed verification is reported as a server error', async function (assert) {+ const failure = new Error('verification service down');+ class FetchStub extends Service {+ post() {+ return Promise.reject(failure);+ }+ }+ class NotificationsStub extends Service {+ serverErrors = [];+ success() {}+ info() {}+ serverError(error) {+ this.serverErrors.push(error);+ }+ }+ this.owner.register('service:fetch', FetchStub);+ this.owner.register('service:notifications', NotificationsStub);++ const controller = this.owner.lookup('controller:onboard/verify-email');+ let transitioned = false;+ Object.defineProperty(controller.router, 'transitionTo', { configurable: true, value: () => (transitioned = true) });++ await controller.verifyCode.perform();++ assert.deepEqual(this.owner.lookup('service:notifications').serverErrors, [failure]);+ assert.false(transitioned, 'the user stays on the verification screen');+ });+});diff --git a/console/tests/unit/controllers/tracked-defaults-test.js b/console/tests/unit/controllers/tracked-defaults-test.jsnew file mode 100644index 00000000..0d54a957--- /dev/null+++ b/console/tests/unit/controllers/tracked-defaults-test.js@@ -0,0 +1,63 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';+import Service from '@ember/service';++/**+ * A @tracked field's initializer only runs the first time the property is read, so a+ * controller whose defaults are never read has them recorded as uncovered. These assert+ * the starting state each screen presents before anything writes to it — which is real+ * behaviour: it is what the template binds to on first paint.+ */+const CONTROLLERS = [+ {+ name: 'controller:auth/login',+ defaults: { isValidating: false, isLoading: false, isSlowConnection: false, timeout: null },+ },+ {+ name: 'controller:auth/two-fa',+ defaults: { otpValue: '', countdownReady: false, isCodeExpired: false, verificationCode: '' },+ },+];++module('Unit | Controller | tracked defaults', function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ // Several of these controllers reach for fetch as soon as they are built.+ class FetchStub extends Service {+ get() {+ return Promise.resolve({});+ }+ post() {+ return Promise.resolve({});+ }+ }+ this.owner.register('service:fetch', FetchStub);+ });++ CONTROLLERS.forEach(({ name, defaults }) => {+ test(`${name} starts in its documented default state`, function (assert) {+ const controller = this.owner.lookup(name);++ for (const [property, value] of Object.entries(defaults)) {+ assert.strictEqual(controller[property], value, property);+ }+ });+ });++ test('auth/verification starts ready-to-submit off and exposes its query params', function (assert) {+ // Looked up without settling: this controller schedules a 75s timer in its+ // constructor which willDestroy cancels at teardown.+ const controller = this.owner.lookup('controller:auth/verification');++ assert.false(controller.isReadyToSubmit, 'the submit button is disabled until a code is typed');+ assert.false(controller.stillWaiting);+ assert.deepEqual(controller.queryParams, ['hello', 'token', 'code']);+ });++ test('onboard/verify-email exposes its query params', function (assert) {+ const controller = this.owner.lookup('controller:onboard/verify-email');++ assert.deepEqual(controller.queryParams, ['hello', 'code']);+ });+});