import { TABLE } from '@adalo/constants' import { IObject } from 'interfaces' import { isScreenManagedDependency } from 'utils/dependencies/dataBindings' import { getId } from 'utils/ids' import { deepMerge } from 'utils/objects' type Component = Parameters[0] type Dependency = Parameters[1] const defaultTableAttributes = { name: 'Table', height: 100, width: 100, x: 0, y: 0, } const buildTableObject = (obj: Partial): IObject => deepMerge( { id: obj.id ?? getId(), type: TABLE, attributes: obj.attributes ?? defaultTableAttributes, children: [], layout: {}, }, obj ) describe('isScreenManagedDependency()', () => { test(`returns false if the dependency has the table object id within it's bindings`, () => { const tableObjectId = 'adf819' const screen = { id: 'screen-1924888', objects: [ buildTableObject({ id: tableObjectId }), buildTableObject({ id: '194-fdd2' }), // random table ], } const mockDependency1 = { bindingId: [tableObjectId] } const mockDependency2 = { bindingIds: [tableObjectId] } const mockDependency3 = { bindingId: tableObjectId } const mockDependency4 = { bindingIds: ['19400addf', tableObjectId, '349aaaaf3', 'aa300sff3'], } expect(isScreenManagedDependency(screen, mockDependency1)).toBe(false) expect(isScreenManagedDependency(screen, mockDependency2)).toBe(false) expect(isScreenManagedDependency(screen, mockDependency3)).toBe(false) expect(isScreenManagedDependency(screen, mockDependency4)).toBe(false) }) test(`returns true if the dependency does not have the table object id within it's bindings`, () => { const bindingId = 'adf819' const screen = { id: 'screen-29188', objects: [ buildTableObject({ id: 'jfa8d91113' }), buildTableObject({ id: 'fff1839' }), buildTableObject({ id: 'auf881439' }), ], } const mockDependency1 = { bindingId: [bindingId] } const mockDependency2 = { bindingIds: [bindingId] } const mockDependency3 = { bindingId } const mockDependency4 = { bindingIds: ['19400addf', '349aaaaf3', 'aa300sff3'], } expect(isScreenManagedDependency(screen, mockDependency1)).toBe(true) expect(isScreenManagedDependency(screen, mockDependency2)).toBe(true) expect(isScreenManagedDependency(screen, mockDependency3)).toBe(true) expect(isScreenManagedDependency(screen, mockDependency4)).toBe(true) }) // These mocks were pulled from actual screens & dependencies, and trimmed down for the tests const mockScreenWithTable: Component = { name: 'Screen', type: 'component', id: '9l9wm2onp3z9ltpokuo7euzlx', objects: [ { id: 'er2drfa4yi2pfh6kw3lbwsoid', name: 'Table', type: 'table', dataBinding: { id: '2e4adevxgnymb9tjoalzvg2z3', type: 'binding', source: { sort: null, type: 'data', source: null, tableId: 't_9aujyc1yk6rnb8uuy1m037cqr', dataType: 'list', datasourceId: 'fc93849a0a0f4cbab85ab97ddd4234b5', }, options: { paginate: true, }, bindingType: 'list', }, positioning: 'fixed', responsivity: { verticalScaling: 'FIXED', horizontalScaling: 'SCALES_WITH_PARENT', verticalPositioning: 'TOP', horizontalPositioning: 'CENTER', }, initialDevice: 'desktop', orderedFields: [ 'c_e72xwttcmzuogc063a0ys9s34', 'c_8xh7vk8d6000982l64k0ji8so', 'c_6e4am74rym3tkh0z60n7aljgx', 'c_dtp0exkqf3bq1zbj12wp0b0u1', 'c_dpljfofa42tuzapri57un31ua', 'c_d35ug14yohgb2obq6glonw0ze', ], }, ], dataBindings: { er2drfa4yi2pfh6kw3lbwsoid: { id: '2e4adevxgnymb9tjoalzvg2z3', type: 'binding', source: { sort: null, type: 'data', source: null, tableId: 't_9aujyc1yk6rnb8uuy1m037cqr', dataType: 'list', datasourceId: 'fc93849a0a0f4cbab85ab97ddd4234b5', }, options: { paginate: true }, bindingType: 'list', childBindings: [], siblingBindings: [ 'd3fb6vs68q7r17s4h4ctrib60', 'e9sq86nlbevlu1n3biffai18t', 'ej3pu27ta1dg6y7r3888zrzk9', '9weutarmau3vnlh3gq113ryoh', '8v3c5hl80omg541oblvgam8wv', '6imwlvca5gbpvr21mn8osc00u', ], }, }, } const mockDependencyForScreenWithTable: Dependency = { isExternalCollection: false, datasourceId: 'fc93849a0a0f4cbab85ab97ddd4234b5', tableId: 't_9aujyc1yk6rnb8uuy1m037cqr', id: null, filter: null, counts: [], includes: [], sort: null, sortReference: '', limit: null, routeParams: {}, paginate: true, columnFilter: '', apiFilter: '', apiParams: {}, isCurrentUser: false, bindingId: ['er2drfa4yi2pfh6kw3lbwsoid'], appId: '82c0c6ec-78ad-41ec-86d3-2089a13ee368', } const mockScreenWithList: Component = { id: '9l9wm2onp3z9ltpokuo7euzlx', name: 'Screen', type: 'component', objects: [ { id: 'djt6h4d5n1juxbf646y8mrsf7', name: 'List', type: 'list', children: [ { id: '2ld7gar1wa26zr0sgm3ic7yvb', name: 'Rectangle', type: 'section', opacity: 1, children: [ { id: '3fbrx23gcr19c7fm3rj8go45m', text: 'Title', type: 'label' }, { id: '8wtrddvm3nt32eb6p8a23odrl', text: 'Other', type: 'label' }, ], }, ], dataBinding: { id: '0cmg4xjoccn0ro6l0pcu4o66s', type: 'binding', source: { sort: null, type: 'data', source: null, tableId: 't_9aujyc1yk6rnb8uuy1m037cqr', dataType: 'list', datasourceId: 'fc93849a0a0f4cbab85ab97ddd4234b5', }, options: { paginate: true }, bindingType: 'list', }, }, ], dataBindings: { djt6h4d5n1juxbf646y8mrsf7: { id: '0cmg4xjoccn0ro6l0pcu4o66s', type: 'binding', source: { sort: null, type: 'data', source: null, tableId: 't_9aujyc1yk6rnb8uuy1m037cqr', dataType: 'list', datasourceId: 'fc93849a0a0f4cbab85ab97ddd4234b5', }, options: { paginate: true, }, bindingType: 'list', childBindings: [], siblingBindings: [], }, }, } const mockDependencyForScreenWithList: Dependency = { isExternalCollection: false, datasourceId: 'fc93849a0a0f4cbab85ab97ddd4234b5', tableId: 't_9aujyc1yk6rnb8uuy1m037cqr', id: null, filter: null, counts: [], includes: [], sort: null, sortReference: '', limit: null, routeParams: {}, paginate: true, columnFilter: '', apiFilter: '', apiParams: {}, isCurrentUser: false, bindingId: ['djt6h4d5n1juxbf646y8mrsf7'], appId: '82c0c6ec-78ad-41ec-86d3-2089a13ee368', } describe('returns false', () => { test('if the dependency is a table dependency', () => { expect( isScreenManagedDependency( mockScreenWithTable, mockDependencyForScreenWithTable ) ).toBe(false) }) test('if the dependency is a list dependency with manualPagination enabled', () => { const mockScreenWithListWithManualPagination = { ...mockScreenWithList, objects: mockScreenWithList.objects.map(obj => ({ ...obj, dataBinding: { ...obj.dataBinding, options: { ...obj.dataBinding.options, manualPagination: true }, }, })), } expect( isScreenManagedDependency( mockScreenWithListWithManualPagination, mockDependencyForScreenWithList ) ).toBe(true) }) }) describe('returns true', () => { test('if the dependency is a list dependency with manualPagination enabled', () => { expect( isScreenManagedDependency( mockScreenWithList, mockDependencyForScreenWithList ) ).toBe(true) }) }) test(`dependencies for external collections don't are managed by the screen`, () => { const externalCollectionDependency = { ...mockDependencyForScreenWithTable, isExternalCollection: true, } expect( isScreenManagedDependency( mockScreenWithTable, externalCollectionDependency ) ).toBe(true) }) })