|
| 1 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 2 | +import { articleService, setGlobalConfig } from '@sdk/dist'; |
| 3 | + |
| 4 | +describe('filter', () => { |
| 5 | + let capturedRequest: Request; |
| 6 | + |
| 7 | + beforeAll(() => { |
| 8 | + setGlobalConfig({ |
| 9 | + host: 'test.example.com', |
| 10 | + secure: true, |
| 11 | + key: 'test-key', |
| 12 | + interceptors: { |
| 13 | + request: (request) => { |
| 14 | + capturedRequest = request; |
| 15 | + return new Response( |
| 16 | + JSON.stringify({ |
| 17 | + result: [], |
| 18 | + referencedEntities: {}, |
| 19 | + additionalProperties: {} |
| 20 | + }), |
| 21 | + { |
| 22 | + status: 200, |
| 23 | + headers: { 'content-type': 'application/json' } |
| 24 | + } |
| 25 | + ); |
| 26 | + } |
| 27 | + } |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('some query', () => { |
| 32 | + it('should assemble a typed filter object into individual query params', async () => { |
| 33 | + const service = articleService(); |
| 34 | + |
| 35 | + await service.some({ |
| 36 | + filter: { |
| 37 | + articleNumber: { EQ: 'ART-001' } |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + const url = new URL(capturedRequest.url); |
| 42 | + expect(url.searchParams.get('articleNumber-eq')).toBe('ART-001'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should pass sort as a comma-separated query param', async () => { |
| 46 | + const service = articleService(); |
| 47 | + |
| 48 | + await service.some({ |
| 49 | + sort: [{ articleNumber: 'asc' }, { name: 'desc' }] |
| 50 | + }); |
| 51 | + |
| 52 | + const url = new URL(capturedRequest.url); |
| 53 | + expect(url.searchParams.get('sort')).toBe('articleNumber,-name'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should pass pagination as page and pageSize query params', async () => { |
| 57 | + const service = articleService(); |
| 58 | + |
| 59 | + await service.some({ |
| 60 | + pagination: { page: 3, pageSize: 25 } |
| 61 | + }); |
| 62 | + |
| 63 | + const url = new URL(capturedRequest.url); |
| 64 | + expect(url.searchParams.get('page')).toBe('3'); |
| 65 | + expect(url.searchParams.get('pageSize')).toBe('25'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should combine filter, sort and pagination into a single request', async () => { |
| 69 | + const service = articleService(); |
| 70 | + |
| 71 | + await service.some({ |
| 72 | + filter: { |
| 73 | + articleNumber: { EQ: 'ART-001' } |
| 74 | + }, |
| 75 | + sort: [{ articleNumber: 'asc' }], |
| 76 | + pagination: { page: 1, pageSize: 10 } |
| 77 | + }); |
| 78 | + |
| 79 | + const url = new URL(capturedRequest.url); |
| 80 | + expect(url.searchParams.get('articleNumber-eq')).toBe('ART-001'); |
| 81 | + expect(url.searchParams.get('sort')).toBe('articleNumber'); |
| 82 | + expect(url.searchParams.get('page')).toBe('1'); |
| 83 | + expect(url.searchParams.get('pageSize')).toBe('10'); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('count query', () => { |
| 88 | + it('should assemble a typed filter object into individual query params', async () => { |
| 89 | + const service = articleService(); |
| 90 | + |
| 91 | + await service.count({ |
| 92 | + filter: { |
| 93 | + articleNumber: { EQ: 'ART-001' } |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + const url = new URL(capturedRequest.url); |
| 98 | + expect(url.searchParams.get('articleNumber-eq')).toBe('ART-001'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + afterAll(() => { |
| 103 | + setGlobalConfig(undefined); |
| 104 | + }); |
| 105 | +}); |
0 commit comments