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 admin organization routes
Ronald A. Richardson4f55b4a3488b
diff --git a/console/tests/unit/routes/console/admin/organizations/details-test.js b/console/tests/unit/routes/console/admin/organizations/details-test.jsnew file mode 100644index 00000000..6ef75a2d--- /dev/null+++ b/console/tests/unit/routes/console/admin/organizations/details-test.js@@ -0,0 +1,170 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';+import Service from '@ember/service';++module('Unit | Route | console/admin/organizations/details', function (hooks) {+ setupTest(hooks);++ test('model fetches the organization by public id, normalized into the store', async function (assert) {+ const requests = [];+ class FetchStub extends Service {+ get(path, query, options) {+ requests.push({ path, query, options });+ return Promise.resolve({ id: 'co_1' });+ }+ }+ this.owner.register('service:fetch', FetchStub);++ const route = this.owner.lookup('route:console/admin/organizations/details');+ const model = await route.model({ public_id: 'company_abc' });++ assert.deepEqual(requests, [+ {+ path: 'companies/find/company_abc',+ query: {},+ options: { normalizeToEmberData: true, normalizeModelType: 'company' },+ },+ ]);+ assert.deepEqual(model, { id: 'co_1' });+ });+});++module('Unit | Route | console/admin/organizations/details/activity', function (hooks) {+ setupTest(hooks);++ test('model reuses the organization already loaded by the parent route', function (assert) {+ const route = this.owner.lookup('route:console/admin/organizations/details/activity');+ const organization = { id: 'co_1' };+ let askedFor;+ route.modelFor = (name) => {+ askedFor = name;+ return organization;+ };++ assert.strictEqual(route.model(), organization, 'no second request is made');+ assert.strictEqual(askedFor, 'console.admin.organizations.details');+ });+});++module('Unit | Route | console/admin/organizations/details/extensions', function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ this.response = { extensions: [{ id: 'ext_1' }] };+ const context = this;+ class FetchStub extends Service {+ get(path) {+ context.requested = path;+ return Promise.resolve(context.response);+ }+ }+ this.owner.register('service:fetch', FetchStub);++ this.build = () => {+ const route = this.owner.lookup('route:console/admin/organizations/details/extensions');+ route.modelFor = () => ({ uuid: 'co-uuid-1' });+ return route;+ };+ });++ test('model loads the extensions installed for the organization', async function (assert) {+ const model = await this.build().model();++ assert.strictEqual(this.requested, 'companies/co-uuid-1/extensions');+ assert.deepEqual(model.extensions, [{ id: 'ext_1' }]);+ assert.deepEqual(model.organization, { uuid: 'co-uuid-1' }, 'the organization travels with them');+ });++ test('a response with no extensions yields an empty list', async function (assert) {+ this.response = {};++ const model = await this.build().model();++ assert.deepEqual(model.extensions, [], 'the template can iterate safely');+ });+});++module('Unit | Route | console/admin/organizations/details/extensions-tab', function (hooks) {+ setupTest(hooks);++ test('model pairs the organization with the requested tab slug', function (assert) {+ const route = this.owner.lookup('route:console/admin/organizations/details/extensions-tab');+ const organization = { uuid: 'co-uuid-1' };+ route.modelFor = () => organization;++ const model = route.model({ slug: 'fleet-ops' });++ assert.strictEqual(model.organization, organization);+ assert.strictEqual(model.slug, 'fleet-ops');+ });++ test('setupController exposes both to the template', function (assert) {+ const route = this.owner.lookup('route:console/admin/organizations/details/extensions-tab');+ const organization = { uuid: 'co-uuid-1' };+ const controller = {};++ route.setupController(controller, { organization, slug: 'fleet-ops' });++ assert.strictEqual(controller.organization, organization);+ assert.strictEqual(controller.slug, 'fleet-ops');+ });+});++module('Unit | Route | console/admin/organizations/details/users', function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ this.response = { users: [{ uuid: 'user_1' }], meta: { total: 1 } };+ const context = this;+ class FetchStub extends Service {+ get(path, query) {+ context.request = { path, query };+ return Promise.resolve(context.response);+ }+ jsonToModel(json, modelName) {+ return { ...json, modelName };+ }+ }+ this.owner.register('service:fetch', FetchStub);++ this.build = () => {+ const route = this.owner.lookup('route:console/admin/organizations/details/users');+ route.modelFor = () => ({ public_id: 'company_abc' });+ return route;+ };+ });++ test('every nested list param refreshes the model', function (assert) {+ const route = this.build();++ for (const param of ['nestedPage', 'nestedLimit', 'nestedSort', 'nestedQuery']) {+ assert.true(route.queryParams[param].refreshModel, `${param} refreshes`);+ }+ });++ test('model requests a paginated page of the organization members', async function (assert) {+ const model = await this.build().model({ nestedPage: 2, nestedLimit: 10, nestedSort: '-created_at', nestedQuery: 'ron' });++ assert.strictEqual(this.request.path, 'companies/company_abc/users');+ assert.deepEqual(this.request.query, { page: 2, limit: 10, sort: '-created_at', query: 'ron', paginate: 1 });+ assert.deepEqual(model.meta, { total: 1 }, 'the paging meta is kept for the table');+ assert.deepEqual(model.toArray(), [{ uuid: 'user_1', modelName: 'user' }], 'each row is turned into a user model');+ });++ test('a response that is not a list is passed through untouched', async function (assert) {+ this.response = { users: undefined, meta: {} };++ const model = await this.build().model({});++ assert.strictEqual(model.get('content'), undefined, 'nothing is mapped');+ });++ test('setupController hands the organization to the controller', function (assert) {+ const route = this.build();+ const controller = {};++ route.setupController(controller);++ assert.deepEqual(controller.company, { public_id: 'company_abc' });+ });+});diff --git a/console/tests/unit/routes/console/admin/organizations/index-test.js b/console/tests/unit/routes/console/admin/organizations/index-test.jsnew file mode 100644index 00000000..1d8fed5a--- /dev/null+++ b/console/tests/unit/routes/console/admin/organizations/index-test.js@@ -0,0 +1,43 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';++module('Unit | Route | console/admin/organizations/index | query', function (hooks) {+ setupTest(hooks);++ test('every filter param refreshes the model', function (assert) {+ const route = this.owner.lookup('route:console/admin/organizations/index');+ const params = [+ 'page',+ 'query',+ 'sort',+ 'limit',+ 'name',+ 'country',+ 'status',+ 'owner_email',+ 'onboarding_completed',+ 'billing_status',+ 'created_at',+ 'needs_attention',+ 'missing_owner',+ 'inactive_status',+ ];++ for (const param of params) {+ assert.true(route.queryParams[param]?.refreshModel, `${param} refreshes the list`);+ }+ });++ test('model queries companies through the admin view with the current filters', function (assert) {+ const route = this.owner.lookup('route:console/admin/organizations/index');+ const queries = [];+ route.store.query = (type, params) => {+ queries.push({ type, params });+ return Promise.resolve([]);+ };++ route.model({ page: 2, status: 'active' });++ assert.deepEqual(queries, [{ type: 'company', params: { view: 'admin', page: 2, status: 'active' } }]);+ });+});diff --git a/console/tests/unit/routes/console/admin/organizations/index/users-test.js b/console/tests/unit/routes/console/admin/organizations/index/users-test.jsindex fdadbbf8..254fec54 100644--- a/console/tests/unit/routes/console/admin/organizations/index/users-test.js+++ b/console/tests/unit/routes/console/admin/organizations/index/users-test.js@@ -2,73 +2,65 @@ import { module, test } from 'qunit';import { setupTest } from '@fleetbase/console/tests/helpers';import Service from '@ember/service';-module('Unit | Route | console/admin/organizations/index/users', function (hooks) {+module('Unit | Route | console/admin/organizations/index/users | nested list', function (hooks) {setupTest(hooks);- test('every nested query param refreshes the model', function (assert) {- const route = this.owner.lookup('route:console/admin/organizations/index/users');-- assert.deepEqual(Object.keys(route.queryParams), ['nestedPage', 'nestedLimit', 'nestedSort', 'nestedQuery']);- Object.entries(route.queryParams).forEach(([key, options]) => {- assert.true(options.refreshModel, `${key} refreshes the model`);- });- });-- test('model fetches the company users with the nested params mapped through', async function (assert) {- let requested;+ hooks.beforeEach(function () {+ this.response = { users: [{ uuid: 'user_1' }], meta: { total: 1 } };+ const context = this;class FetchStub extends Service {- get(path, params) {- requested = { path, params };- return Promise.resolve({ users: [], meta: { total: 0 } });+ get(path, query) {+ context.request = { path, query };+ return Promise.resolve(context.response);}- jsonToModel(json) {- return json;+ jsonToModel(json, modelName) {+ return { ...json, modelName };}}this.owner.register('service:fetch', FetchStub);+ this.store = this.owner.lookup('service:store');+ this.route = () => this.owner.lookup('route:console/admin/organizations/index/users');+ });- const route = this.owner.lookup('route:console/admin/organizations/index/users');- await route.model({ public_id: 'company_1', nestedPage: 2, nestedLimit: 25, nestedSort: '-name', nestedQuery: 'ron' });+ test('model requests the members of the organization in the URL', async function (assert) {+ const model = await this.route().model({ public_id: 'company_abc', nestedPage: 1, nestedLimit: 20, nestedSort: '-created_at', nestedQuery: '' });- assert.strictEqual(requested.path, 'companies/company_1/users');- assert.deepEqual(requested.params, { page: 2, limit: 25, sort: '-name', query: 'ron', paginate: 1 });- assert.strictEqual(route.companyId, 'company_1', 'the company id is retained for setupController');+ assert.strictEqual(this.request.path, 'companies/company_abc/users');+ assert.deepEqual(this.request.query, { page: 1, limit: 20, sort: '-created_at', query: '', paginate: 1 });+ assert.deepEqual(model.toArray(), [{ uuid: 'user_1', modelName: 'user' }]);+ assert.deepEqual(model.meta, { total: 1 });});- test('transformResults maps users into models and preserves meta', function (assert) {- class FetchStub extends Service {- jsonToModel(json, type) {- return { ...json, _type: type };- }- }- this.owner.register('service:fetch', FetchStub);+ test('a response that is not a list is passed through untouched', async function (assert) {+ this.response = { users: null, meta: {} };- const route = this.owner.lookup('route:console/admin/organizations/index/users');- const proxy = route.transformResults({ users: [{ id: 'u1' }], meta: { total: 1 } });+ const model = await this.route().model({ public_id: 'company_abc' });- assert.deepEqual(proxy.get('meta'), { total: 1 });- assert.deepEqual(proxy.get('content'), [{ id: 'u1', _type: 'user' }]);+ assert.strictEqual(model.get('content'), null);});- test('transformResults passes non-array users through untouched', function (assert) {- const route = this.owner.lookup('route:console/admin/organizations/index/users');- const proxy = route.transformResults({ users: undefined, meta: {} });+ test('getCompany finds the loaded organization by public id or id', async function (assert) {+ const route = this.route();+ this.store.push({ data: { id: 'co_1', type: 'company', attributes: { public_id: 'company_abc' } } });- assert.strictEqual(proxy.get('content'), undefined);- });+ await route.model({ public_id: 'company_abc' });+ assert.strictEqual(route.getCompany()?.id, 'co_1', 'matched on public_id');- test('getCompany matches the loaded company by public_id or id', function (assert) {- const route = this.owner.lookup('route:console/admin/organizations/index/users');- const store = this.owner.lookup('service:store');- store.push({ data: { id: 'uuid-1', type: 'company', attributes: { public_id: 'company_1' } } });+ await route.model({ public_id: 'co_1' });+ assert.strictEqual(route.getCompany()?.id, 'co_1', 'matched on the record id');++ await route.model({ public_id: 'not-loaded' });+ assert.strictEqual(route.getCompany(), undefined, 'an unknown organization yields nothing');+ });- route.companyId = 'company_1';- assert.strictEqual(route.getCompany().public_id, 'company_1', 'matched by public_id');+ test('setupController hands the resolved organization to the controller', async function (assert) {+ const route = this.route();+ this.store.push({ data: { id: 'co_1', type: 'company', attributes: { public_id: 'company_abc' } } });+ await route.model({ public_id: 'company_abc' });+ const controller = {};- route.companyId = 'uuid-1';- assert.strictEqual(route.getCompany().id, 'uuid-1', 'matched by id');+ route.setupController(controller);- route.companyId = 'nope';- assert.strictEqual(route.getCompany(), undefined, 'no match returns undefined');+ assert.strictEqual(controller.company?.id, 'co_1');});});