jest.mock('../services/edocu/elements') import { ElementData, QueryCriteria, RawCriteria } from '../interfaces' import { setupClient } from '../setup.client' import { MainStore } from '../stores' import { DynamicContext, parseDynamicValue } from './parseDynamicValue' const local = 'https://localhost/' let mainStore: MainStore const elementDataBase = { _type: 'Car', organization: 'org', timestamp: new Date().toISOString(), hash: 'hash', } beforeEach(async () => { jest.clearAllMocks() mainStore = setupClient({ stage: 'stage', baseUrl: local, }) await mainStore.initializePlatformServices() }) describe('creates correct element instance for type', () => { it('should parse dynamic value of array of string', async () => { const criteria = { color: '${this.color}', } const elementData = { color: { value: ['red', 'blue', 'yellow'] }, } const expected = { 'color.value': { $in: elementData.color.value, }, } testParsedCriteria( criteria, elementData as unknown as ElementData, expected, ) }) it('should parse dynamic value of one attribute', async () => { const criteria = { color: '${this.color}', } const elementData = { color: { value: 'red' }, } const expected = { 'color.value': elementData.color.value, } testParsedCriteria( criteria, elementData as unknown as ElementData, expected, ) }) it('should parse dynamic value of 2 attribute values in one criterion', async () => { const criteria = { color: '${this.color} a ${this.city}', } const elementData = { color: { value: 'red' }, city: { value: 'Poprad' }, } const expected = { 'color.value': 'red a Poprad', } testParsedCriteria( criteria, elementData as unknown as ElementData, expected, ) }) it('should parse dynamic value of 2 attribute values in one $or criterion', async () => { const criteria: QueryCriteria = { $or: [ { color: '${this.color}', }, { city: '${this.city}', }, ], } const elementData = { color: { value: ['red', 'blue', 'yellow'] }, city: { value: 'Poprad' }, } const expected: RawCriteria = { $or: [ { 'color.value': { $in: elementData.color.value, }, }, { 'city.value': 'Poprad', }, ], } testParsedCriteria( criteria, elementData as unknown as ElementData, expected, ) }) it('should parse dynamic value in date range criterion with timerange', async () => { const criteria: QueryCriteria = { timestamp: '${timerange.from(this.from_date).to(this.to_date)}', } const elementData = { from_date: { value: '2026-05-01T08:00:00.000Z' }, to_date: { value: '2026-05-31T18:30:00.000Z' }, } const expected: RawCriteria = { timestamp: { $gte: '2026-05-01T08:00:00.000Z', $lte: '2026-05-31T18:30:00.000Z', }, } testParsedCriteria( criteria, elementData as unknown as ElementData, expected, ) }) function testParsedCriteria( criteria: QueryCriteria, elementData: ElementData, expected: RawCriteria, ) { const element = mainStore.elementStore.instantiateElement('213', { ...elementDataBase, ...elementData, } as unknown as ElementData) const context: DynamicContext = { element, uid: 'misob', } const parsedCriteria = mainStore.listing.parseCriteria(criteria, context) expect(parsedCriteria).toEqual(expected) } }) describe('parseDynamicValue (listing-style options and forceOutputQueryFormat)', () => { function elementWithData(data: Record) { return mainStore.elementStore.instantiateElement('dyn-1', { ...elementDataBase, ...data, } as unknown as ElementData) } it('whole template only ${this.attr} with multi-value returns {$in} object (same as parseCriteria)', () => { const element = elementWithData({ color: { value: ['red', 'blue'] }, }) const out = parseDynamicValue( '${this.color}', { element }, { allowObjectNotation: true, }, ) expect(out).toEqual({ $in: ['red', 'blue'] }) }) it('two primitive placeholders in one string still merge into one string', () => { const element = elementWithData({ color: { value: 'red' }, city: { value: 'Poprad' }, }) const out = parseDynamicValue('${this.color} / ${this.city}', { element }) expect(out).toBe('red / Poprad') }) })