refactor: 💡 Remove usage of indexed DB (#2979)

* refactor: 💡 Remove usage of indexed DB
This commit is contained in:
Zhihe Li committed 2025-09-03 11:41:43 -04:00
1 parent 4c7ea161cd
commit 9e263379a7
101 files changed
+15 -2682

No files matched your search

@@ -1,609 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { queryIndexedDb } from 'api/utils/indexed-db-query';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { setupIndexedDb } from 'api/test-support/helpers/indexed-db';
import { pluralize } from 'ember-inflector';
import { camelize } from '@ember/string';
import { TYPE_TARGET_TCP, TYPE_TARGET_SSH } from 'api/models/target';
import { faker } from '@faker-js/faker';
const seedIndexDb = async (resource, store, indexedDb, server) => {
const resourceData =
server.schema[pluralize(camelize(resource))].all().models;
const serializedData = resourceData.map((resource) =>
JSON.parse(JSON.stringify(server.serializerOrRegistry.serialize(resource))),
);
const serializer = store.serializerFor(resource);
const schema = store.modelFor(resource);
const normalizedPayload = serializer.normalizeResponse(
store,
schema,
{ items: serializedData },
null,
'query',
);
await indexedDb.db[resource].bulkPut(
normalizedPayload.data.map((datum) =>
indexedDb.normalizeData({
data: datum,
cleanData: true,
schema,
serializer,
}),
),
);
};
module('Unit | Utility | indexed-db-query', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
setupIndexedDb(hooks);
let indexedDb;
let store;
let scope1;
let scope2;
const resource = 'target';
hooks.beforeEach(function () {
indexedDb = this.owner.lookup('service:indexed-db');
store = this.owner.lookup('service:store');
scope1 = this.server.create('scope', {
id: 'p_123',
type: 'project',
});
scope2 = this.server.create('scope', {
id: 'p_456',
type: 'project',
});
this.server.createList(resource, 5, {
type: TYPE_TARGET_SSH,
scopeId: scope1.id,
});
this.server.createList(resource, 5, {
type: TYPE_TARGET_TCP,
scopeId: scope2.id,
});
});
test('it filters by single field', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: [{ equals: TYPE_TARGET_SSH }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by single field with "and" logicalOperator', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: {
logicalOperator: 'and',
values: [{ equals: TYPE_TARGET_SSH }],
},
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by single field with "or" logicalOperator', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: {
logicalOperator: 'or',
values: [{ equals: TYPE_TARGET_SSH }, { equals: TYPE_TARGET_TCP }],
},
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 10);
});
test('it filters by multiple fields', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: [{ equals: TYPE_TARGET_SSH }],
scope_id: [{ equals: scope1.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by multiple fields with "or" logicalOperator', async function (assert) {
this.server.create(resource, { type: TYPE_TARGET_TCP, scopeId: scope1.id });
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: {
logicalOperator: 'or',
values: [{ equals: TYPE_TARGET_SSH }, { equals: TYPE_TARGET_TCP }],
},
scope_id: [{ equals: scope1.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 6);
});
test('it filters by multiple fields with "and" logicalOperator', async function (assert) {
this.server.create(resource, { type: TYPE_TARGET_TCP, scopeId: scope1.id });
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: {
logicalOperator: 'and',
values: [{ equals: TYPE_TARGET_SSH }, { notEquals: TYPE_TARGET_TCP }],
},
scope_id: [{ equals: scope1.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by multiple fields and uses two "or" logicalOperators', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: {
logicalOperator: 'or',
values: [{ equals: TYPE_TARGET_SSH }, { equals: TYPE_TARGET_TCP }],
},
scope_id: {
logicalOperator: 'or',
values: [{ equals: scope1.id }, { equals: scope2.id }],
},
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 10);
});
test('it filters by multiple fields and uses two "and" logicalOperators', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: {
logicalOperator: 'and',
values: [{ notEquals: TYPE_TARGET_SSH }, { equals: TYPE_TARGET_TCP }],
},
scope_id: {
logicalOperator: 'and',
values: [{ equals: scope2.id }, { notEquals: scope1.id }],
},
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it cannot filter with "contains" as initial operation', async function (assert) {
assert.expect(1);
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: [{ contains: scope1.id }],
},
};
try {
await queryIndexedDb(indexedDb.db, resource, query);
} catch (e) {
assert.strictEqual(
e.message,
'Contains is not supported as the first filter.',
);
}
});
test('it cannot filter with "contains" as subsequent operation', async function (assert) {
assert.expect(2);
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: {
logicalOperator: 'and',
values: [{ equals: scope1.id }, { contains: scope2.id.slice(0, 2) }],
},
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
query.filters.scope_id.logicalOperator = 'or';
try {
await queryIndexedDb(indexedDb.db, resource, query);
} catch (e) {
assert.strictEqual(
e.message,
'Contains is not supported as a filter option with "or" operator.',
);
}
});
// TODO: Modify test cases that use comparison opertators to compare fields that
// are of type number. Additionally, add test case for searching on boolean fields.
test('it filters by single field with "gt" as initial operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: [{ gt: scope1.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by single field with "gt" as subsequent operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: {
logicalOperator: 'and',
values: [{ equals: scope2.id }, { gt: scope1.id }],
},
},
};
let result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
query.filters.scope_id.logicalOperator = 'or';
result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by single field with "gte" as initial operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: [{ gte: scope1.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 10);
});
test('it filters by single field with "gte" as subsequent operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: {
logicalOperator: 'and',
values: [{ equals: scope2.id }, { gte: scope1.id }],
},
},
};
let result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
query.filters.scope_id.logicalOperator = 'or';
result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 10);
});
test('it filters by single field with "lt" as initial operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: [{ lt: scope2.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by single field with "lt" as subsequent operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: {
logicalOperator: 'and',
values: [{ equals: scope1.id }, { lt: scope2.id }],
},
},
};
let result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
query.filters.scope_id.logicalOperator = 'or';
result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by single field with "lte" as initial operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: [{ lte: scope2.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 10);
});
test('it filters by single field with "lte" as subsequent operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: {
logicalOperator: 'and',
values: [{ equals: scope1.id }, { lte: scope2.id }],
},
},
};
let result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
query.filters.scope_id.logicalOperator = 'or';
result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 10);
});
test('it filters by single field with "notEquals" as initial operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: [{ notEquals: scope1.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by single field with "notEquals" as subsequent operation', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
scope_id: {
logicalOperator: 'and',
values: [{ equals: scope2.id }, { notEquals: scope1.id }],
},
},
};
let result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
query.filters.scope_id.logicalOperator = 'or';
result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters and returns zero results', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
filters: {
type: [{ equals: 'unknown' }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 0);
});
test('it searches with a query that matches a field exactly', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = { search: scope1.id };
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it searches with a query that matches a field partially', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = { search: scope2.id.slice(0, 2) };
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 10);
});
test('it searches and returns no results with a query that matches no fields', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = { search: `${scope1.id}456789` };
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 0);
});
test('it filters by a single field and searches', async function (assert) {
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
search: scope1.id,
filters: {
type: [{ equals: TYPE_TARGET_SSH }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 5);
});
test('it filters by multiple fields and searches', async function (assert) {
const target = this.server.create(resource, {
name: 'Generated_ssh_target',
type: TYPE_TARGET_SSH,
scopeId: scope1.id,
});
await seedIndexDb(resource, store, indexedDb, this.server);
const query = {
search: target.name,
filters: {
type: [{ equals: TYPE_TARGET_SSH }],
scope_id: [{ equals: scope1.id }],
},
};
const result = await queryIndexedDb(indexedDb.db, resource, query);
assert.strictEqual(result.length, 1);
});
test('it filters on boolean type fields with truthy value', async function (assert) {
const authMethod = this.server.create('auth-method', {
name: 'Generated_auth_method',
scopeId: scope1.id,
});
scope1.update({ primaryAuthMethodId: authMethod.id });
await seedIndexDb('auth-method', store, indexedDb, this.server);
const query = { filters: { is_primary: [{ equals: 'true' }] } };
const result = await queryIndexedDb(indexedDb.db, 'auth-method', query);
assert.strictEqual(result.length, 1);
});
test('it filters on boolean type fields with falsy value', async function (assert) {
const authMethod = this.server.create('auth-method', {
name: 'Generated_auth_method',
scopeId: scope1.id,
});
scope1.update({ primaryAuthMethodId: authMethod.id });
await seedIndexDb('auth-method', store, indexedDb, this.server);
const query = { filters: { is_primary: [{ equals: 'false' }] } };
const result = await queryIndexedDb(indexedDb.db, 'auth-method', query);
assert.strictEqual(result.length, 0);
});
test('it filters on date type fields and returns results', async function (assert) {
const pastDate = new Date('2024-09-19T10:00:00.000Z');
const createdTime = new Date('2024-09-23T10:00:00.000Z');
this.server.create('session-recording', {
created_time: createdTime,
});
await seedIndexDb('session-recording', store, indexedDb, this.server);
const query = { filters: { created_time: [{ gte: pastDate }] } };
const result = await queryIndexedDb(
indexedDb.db,
'session-recording',
query,
);
assert.strictEqual(result.length, 1);
});
test('it filters on date type fields and returns results - from and to', async function (assert) {
const from = new Date('2024-09-19T10:00:00.000Z');
const to = new Date('2024-09-25T10:00:00.000Z');
const createdTime = new Date('2024-09-23T10:00:00.000Z');
const createdTime2 = new Date('2024-09-28T10:00:00.000Z');
this.server.create('session-recording', {
created_time: createdTime,
});
this.server.create('session-recording', {
created_time: createdTime2,
});
await seedIndexDb('session-recording', store, indexedDb, this.server);
const query = {
filters: {
created_time: {
logicalOperator: 'and',
values: [{ gte: from }, { lte: to }],
},
},
};
const result = await queryIndexedDb(
indexedDb.db,
'session-recording',
query,
);
assert.strictEqual(result.length, 1);
assert.strictEqual(
result[0].attributes.created_time.toISOString(),
createdTime.toISOString(),
);
});
test('it filters on date type fields and returns no results', async function (assert) {
const createdTime = faker.date.between({
from: '2024-09-19T00:00:00.000Z',
to: '2024-09-20T00:00:00.000Z',
});
const pastDate = faker.date.between({
from: '2024-09-23T00:00:00.000Z',
to: '2024-09-24T00:00:00.000Z',
});
this.server.create('session-recording', {
created_time: createdTime,
});
await seedIndexDb('session-recording', store, indexedDb, this.server);
const query = { filters: { created_time: [{ gte: pastDate }] } };
const result = await queryIndexedDb(
indexedDb.db,
'session-recording',
query,
);
assert.strictEqual(result.length, 0);
});
});
@@ -1,541 +0,0 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import { assert, module, test } from 'qunit';
import { sortResults } from 'api/utils/sort-results';
import { faker } from '@faker-js/faker';
import { setupTest } from 'ember-qunit';
function createJsonApiMockRecord(id, type, attributes) {
return {
id,
type,
attributes,
};
}
module('Unit | Utility | sortResults', function (hooks) {
setupTest(hooks);
module('Defaults', function () {
test("throws an error when an attribute is not id, created_time or defined as a schema attribute, even when it exists on the record's attribute object", function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
name: 'Result 01',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
name: 'Result 02',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
name: 'Result 03',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
]);
const querySort = { attributes: ['name'] };
// Note: `name` is not defined as a schema attribute but exists on the record
const schema = { attributes: new Map() };
schema.attributes.set('date', { type: 'date' });
assert.throws(
() => sortResults(shuffledResults, { querySort, schema }),
/Error: The attribute "name" does not map to the model definition of type "target". Supported sortable attributes are 'id', 'created_time', or 'date'/,
);
});
test('throws an error when results do not contain a valid sort direction', function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
name: 'Result 01',
});
const querySort = {
attributes: ['name'],
direction: 'invalid sort direction',
};
const schema = { attributes: new Map() };
schema.attributes.set('name', { type: 'string' });
assert.throws(
() => sortResults([result01], { querySort, schema }),
/Error: Invalid sort direction/,
);
});
test('when passing no querySort, sorts using default created_time, descending order and string sorting', async function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
created_time: '2021',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
created_time: '2022',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
created_time: '2023',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
created_time: '2024',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySort = {};
const schema = { attributes: new Map() };
schema.attributes.set('created_time', { type: 'string' });
const sortedResults = sortResults(shuffledResults, {
querySort,
schema,
});
assert.deepEqual(
sortedResults.map(({ attributes: { created_time } }) => created_time),
['2024', '2023', '2022', '2021'],
);
});
test('when sorting by id, without being on the results attributes it sorts as expected ', function () {
const result01 = createJsonApiMockRecord('id_01', 'target', {
name: 'Result 01',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
name: 'Result 02',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
name: 'Result 03',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
name: 'Result 04',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySort = { attributes: ['id'] };
const schema = { attributes: new Map() };
schema.attributes.set('id', { type: 'string' });
const sortedResults = sortResults(shuffledResults, { querySort, schema });
assert.deepEqual(
sortedResults.map(({ id }) => id),
['id_01', 'id_02', 'id_03', 'id_04'],
);
});
});
module('string sorting', function () {
test('it sorts by ascending (default) `name` using string sorting', async function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
name: 'Result 01',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
name: 'Result 02',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
name: 'Result 03',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
name: 'Result 04',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySort = { attributes: ['name'] };
const schema = { attributes: new Map() };
schema.attributes.set('name', { type: 'string' });
const sortedResults = sortResults(shuffledResults, { querySort, schema });
assert.deepEqual(
sortedResults.map(({ attributes: { name } }) => name),
['Result 01', 'Result 02', 'Result 03', 'Result 04'],
);
});
test('it sorts by ascending then descending `name` using string sorting', async function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
name: 'Result 01',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
name: 'Result 02',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
name: 'Result 03',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
name: 'Result 04',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySortAsc = { attributes: ['name'], direction: 'asc' };
const querySortDesc = { attributes: ['name'], direction: 'desc' };
const schema = { attributes: new Map() };
schema.attributes.set('name', { type: 'string' });
const sortedResultsAsc = sortResults(shuffledResults, {
querySort: querySortAsc,
schema,
});
const sortedResultsDesc = sortResults(shuffledResults, {
querySort: querySortDesc,
schema,
});
assert.deepEqual(
sortedResultsAsc.map(({ attributes: { name } }) => name),
['Result 01', 'Result 02', 'Result 03', 'Result 04'],
);
assert.deepEqual(
sortedResultsDesc.map(({ attributes: { name } }) => name),
['Result 04', 'Result 03', 'Result 02', 'Result 01'],
);
});
test('it sorts by ascending then descending `name` using string sorting with Upper case preference', async function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
name: 'orange',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
name: 'Orange',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
name: 'banana',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
name: 'Banana',
});
const result05 = createJsonApiMockRecord('id_04', 'target', {
name: 'apple',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
result05,
]);
const querySortAsc = { attributes: ['name'], direction: 'asc' };
const querySortDesc = { attributes: ['name'], direction: 'desc' };
const schema = { attributes: new Map() };
schema.attributes.set('name', { type: 'string' });
const sortedResultsAsc = sortResults(shuffledResults, {
querySort: querySortAsc,
schema,
});
const sortedResultsDesc = sortResults(shuffledResults, {
querySort: querySortDesc,
schema,
});
assert.deepEqual(
sortedResultsAsc.map(({ attributes: { name } }) => name),
['apple', 'Banana', 'banana', 'Orange', 'orange'],
);
assert.deepEqual(
sortedResultsDesc.map(({ attributes: { name } }) => name),
['orange', 'Orange', 'banana', 'Banana', 'apple'],
);
});
});
module('date sorting', function () {
test('it sorts by ascending (default) `date` using date sorting', function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
date: '2021',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
date: '2022',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
date: '2023',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
date: '2024',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySort = { attributes: ['date'] };
const schema = { attributes: new Map() };
schema.attributes.set('date', { type: 'date' });
const sortedResults = sortResults(shuffledResults, { querySort, schema });
assert.deepEqual(
sortedResults.map(({ attributes: { date } }) => date),
['2021', '2022', '2023', '2024'],
);
});
test('it sorts by ascending then descending `date` using date sorting', function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
date: '2021',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
date: '2022',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
date: '2023',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
date: '2024',
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySortAsc = { attributes: ['date'], direction: 'asc' };
const querySortDesc = { attributes: ['date'], direction: 'desc' };
const schema = { attributes: new Map() };
schema.attributes.set('date', { type: 'date' });
const sortedResultsAsc = sortResults(shuffledResults, {
querySort: querySortAsc,
schema,
});
const sortedResultsDesc = sortResults(shuffledResults, {
querySort: querySortDesc,
schema,
});
assert.deepEqual(
sortedResultsAsc.map(({ attributes: { date } }) => date),
['2021', '2022', '2023', '2024'],
);
assert.deepEqual(
sortedResultsDesc.map(({ attributes: { date } }) => date),
['2024', '2023', '2022', '2021'],
);
});
});
module('number sorting', function () {
test('it sorts by ascending (default) `sessions`', function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
sessions: 10,
});
const result02 = createJsonApiMockRecord('id_01', 'target', {
sessions: 200,
});
const result03 = createJsonApiMockRecord('id_01', 'target', {
sessions: 3333,
});
const result04 = createJsonApiMockRecord('id_01', 'target', {
sessions: 40444,
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySort = { attributes: ['sessions'] };
const schema = { attributes: new Map() };
schema.attributes.set('sessions', { type: 'number' });
const sortedResults = sortResults(shuffledResults, { querySort, schema });
assert.deepEqual(
sortedResults.map(({ attributes: { sessions } }) => sessions),
[10, 200, 3333, 40444],
);
});
test('it sorts by ascending then descending `sessions`', function (assert) {
const result01 = createJsonApiMockRecord('id_01', 'target', {
sessions: 10,
});
const result02 = createJsonApiMockRecord('id_01', 'target', {
sessions: 200,
});
const result03 = createJsonApiMockRecord('id_01', 'target', {
sessions: 3333,
});
const result04 = createJsonApiMockRecord('id_01', 'target', {
sessions: 40444,
});
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySortAsc = { attributes: ['sessions'], direction: 'asc' };
const querySortDesc = { attributes: ['sessions'], direction: 'desc' };
const schema = { attributes: new Map() };
schema.attributes.set('sessions', { type: 'number' });
const sortedResultsAsc = sortResults(shuffledResults, {
querySort: querySortAsc,
schema,
});
const sortedResultsDesc = sortResults(shuffledResults, {
querySort: querySortDesc,
schema,
});
assert.deepEqual(
sortedResultsAsc.map(({ attributes: { sessions } }) => sessions),
[10, 200, 3333, 40444],
);
assert.deepEqual(
sortedResultsDesc.map(({ attributes: { sessions } }) => sessions),
[40444, 3333, 200, 10],
);
});
});
module('boolean sorting', function () {
test('it sorts by ascending (default) `active`', function (assert) {
const result01 = { attributes: { active: true } };
const result02 = { attributes: { active: false } };
const result03 = { attributes: { active: false } };
const result04 = { attributes: { active: true } };
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySort = { attributes: ['active'] };
const schema = { attributes: new Map() };
schema.attributes.set('active', { type: 'boolean' });
const sortedResults = sortResults(shuffledResults, { querySort, schema });
assert.deepEqual(
sortedResults.map(({ attributes: { active } }) => active),
[false, false, true, true],
);
});
test('it sorts by ascending then descending `active`', function (assert) {
const result01 = { attributes: { active: true } };
const result02 = { attributes: { active: false } };
const result03 = { attributes: { active: false } };
const result04 = { attributes: { active: true } };
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
const querySortAsc = { attributes: ['active'], direction: 'asc' };
const querySortDesc = { attributes: ['active'], direction: 'desc' };
const schema = { attributes: new Map() };
schema.attributes.set('active', { type: 'boolean' });
const sortedResultsAsc = sortResults(shuffledResults, {
querySort: querySortAsc,
schema,
});
const sortedResultsDesc = sortResults(shuffledResults, {
querySort: querySortDesc,
schema,
});
assert.deepEqual(
sortedResultsAsc.map(({ attributes: { active } }) => active),
[false, false, true, true],
);
assert.deepEqual(
sortedResultsDesc.map(({ attributes: { active } }) => active),
[true, true, false, false],
);
});
});
module('custom function sorting', function () {
const result01 = createJsonApiMockRecord('id_01', 'target', {
name: 'target1',
});
const result02 = createJsonApiMockRecord('id_02', 'target', {
name: 'target2',
});
const result03 = createJsonApiMockRecord('id_03', 'target', {
name: 'target3',
});
const result04 = createJsonApiMockRecord('id_04', 'target', {
name: 'target4',
});
const nameMap = {
target3: 'Alpha',
target4: 'Beta',
target1: 'Delta',
target2: 'Epsilon',
};
const schema = { attributes: new Map() };
test.each(
'it sorts by `name` using custom sorting function',
{
'ascending (default)': {
querySort: {
attributes: ['name'],
customSort: { attributeMap: nameMap },
},
expectedResults: ['target3', 'target4', 'target1', 'target2'],
},
descending: {
querySort: {
attributes: ['name'],
customSort: { attributeMap: nameMap },
direction: 'desc',
},
expectedResults: ['target2', 'target1', 'target4', 'target3'],
},
ascending: {
querySort: {
attributes: ['name'],
customSort: { attributeMap: nameMap },
direction: 'asc',
},
expectedResults: ['target3', 'target4', 'target1', 'target2'],
},
},
function (assert, input) {
const shuffledResults = faker.helpers.shuffle([
result01,
result02,
result03,
result04,
]);
schema.attributes.set('name', { type: 'string' });
const sortedResults = sortResults(shuffledResults, {
querySort: input.querySort,
schema,
});
assert.deepEqual(
sortedResults.map(({ attributes: { name } }) => name),
input.expectedResults,
);
},
);
});
});