- Updated pagination-response and read-write-controllers documentation to include `orderBy` and `orderType` parameters for sorting results. - Introduced new `order-clause` module to handle ordering logic, including validation for order types and columns. - Enhanced `PaginationQueryDto` to support ordering fields in API requests. - Updated various repository and service classes to implement ordering in database queries. - Added unit tests for new ordering functionality and ensured existing tests cover the updated behavior. - Refactored related DTOs to include user and code relations for better data representation in responses.
94 lines
2.3 KiB
TypeScript
94 lines
2.3 KiB
TypeScript
import {
|
|
CODE_RELATION_FIELDS,
|
|
DEFAULT_RELATION_FIELDS,
|
|
pickCodeRelation,
|
|
pickDefaultRelation,
|
|
pickRelation,
|
|
pickUserRelation,
|
|
USER_RELATION_FIELDS,
|
|
} from './relation-fields';
|
|
|
|
describe('pickRelation', () => {
|
|
const catalog = {
|
|
id: 'div-1',
|
|
code: 'JKT',
|
|
name: 'Jakarta',
|
|
status: 'active',
|
|
extra: 'secret',
|
|
};
|
|
|
|
const user = {
|
|
id: 'user-1',
|
|
username: 'admin',
|
|
passwordHash: 'hashed',
|
|
};
|
|
|
|
it('picks default id, code, name fields', () => {
|
|
expect(pickRelation(catalog, DEFAULT_RELATION_FIELDS)).toEqual({
|
|
id: 'div-1',
|
|
code: 'JKT',
|
|
name: 'Jakarta',
|
|
});
|
|
});
|
|
|
|
it('picks a custom field list for users', () => {
|
|
expect(pickRelation(user, USER_RELATION_FIELDS)).toEqual({
|
|
id: 'user-1',
|
|
username: 'admin',
|
|
});
|
|
});
|
|
|
|
it('returns null for null or undefined sources', () => {
|
|
const missing: typeof catalog | null = null;
|
|
const unset: typeof catalog | undefined = undefined;
|
|
expect(
|
|
pickRelation<typeof catalog, (typeof DEFAULT_RELATION_FIELDS)[number]>(
|
|
missing,
|
|
DEFAULT_RELATION_FIELDS,
|
|
),
|
|
).toBeNull();
|
|
expect(
|
|
pickRelation<typeof catalog, (typeof DEFAULT_RELATION_FIELDS)[number]>(
|
|
unset,
|
|
DEFAULT_RELATION_FIELDS,
|
|
),
|
|
).toBeNull();
|
|
});
|
|
|
|
it('does not copy fields outside the list', () => {
|
|
const picked = pickRelation(catalog, DEFAULT_RELATION_FIELDS);
|
|
expect(picked).not.toHaveProperty('status');
|
|
expect(picked).not.toHaveProperty('extra');
|
|
});
|
|
});
|
|
|
|
describe('pickCodeRelation', () => {
|
|
it('picks id and code only', () => {
|
|
expect(
|
|
pickCodeRelation({
|
|
id: 'so-1',
|
|
code: 'SO-001',
|
|
}),
|
|
).toEqual({ id: 'so-1', code: 'SO-001' });
|
|
expect(CODE_RELATION_FIELDS).toEqual(['id', 'code']);
|
|
expect(pickCodeRelation(null)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('pickDefaultRelation / pickUserRelation', () => {
|
|
it('maps catalog and user sources without leaking extra fields', () => {
|
|
expect(
|
|
pickDefaultRelation({
|
|
id: 'div-1',
|
|
code: 'JKT',
|
|
name: 'Jakarta',
|
|
}),
|
|
).toEqual({ id: 'div-1', code: 'JKT', name: 'Jakarta' });
|
|
expect(pickDefaultRelation(null)).toBeNull();
|
|
expect(pickUserRelation({ id: 'user-1', username: 'admin' })).toEqual({
|
|
id: 'user-1',
|
|
username: 'admin',
|
|
});
|
|
});
|
|
});
|