import { valueFormula } from '@toddledev/core/dist/formula/formulaUtils' import { searchProject } from '../../searchProject' import { unknownApiRule } from './unknownApiRule' describe('unknownApi', () => { test('should report reading unknown apis from formula', () => { const problems = Array.from( searchProject({ files: { components: { test: { name: 'test', nodes: { root: { type: 'element', attrs: {}, classes: { 'my-class': { formula: { type: 'path', path: ['Apis', 'my-api'], }, }, }, events: {}, tag: 'div', children: [], style: {}, }, }, formulas: {}, apis: {}, attributes: {}, variables: {}, }, }, }, rules: [unknownApiRule], }), ) expect(problems).toHaveLength(1) expect(problems[0].code).toBe('unknown api') expect(problems[0].details).toEqual({ name: 'my-api' }) }) test('should report reading unknown apis from actions', () => { const problems = Array.from( searchProject({ files: { formulas: {}, components: { apiComponent: { name: 'test', nodes: { root: { type: 'element', tag: 'button', attrs: {}, style: {}, children: [], classes: {}, events: { onClick: { trigger: 'onClick', actions: [ { type: 'Fetch', api: 'unknown-api', inputs: {}, onSuccess: { actions: [] }, onError: { actions: [] }, }, ], }, }, }, }, formulas: {}, apis: { 'my-api': { name: 'my-api', type: 'http', version: 2, autoFetch: valueFormula(true), inputs: {}, '@toddle/metadata': { comments: null, }, }, }, attributes: {}, variables: {}, }, }, }, rules: [unknownApiRule], }), ) expect(problems).toHaveLength(1) expect(problems[0].code).toBe('unknown api') expect(problems[0].details).toEqual({ name: 'unknown-api' }) }) test('should not report APIs that exist', () => { const problems = Array.from( searchProject({ files: { components: { test: { name: 'test', nodes: { root: { type: 'element', attrs: {}, classes: { 'my-class': { formula: { type: 'path', path: ['Apis', 'my-api', 'data'], }, }, }, events: {}, tag: 'div', children: [], style: {}, }, }, formulas: {}, apis: { 'my-api': { name: 'my-api', type: 'REST', onCompleted: null, onFailed: null, }, }, variables: {}, attributes: { known: { name: 'known', testValue: { type: 'value', value: null }, '@toddle/metadata': { comments: null, }, }, }, }, }, }, rules: [unknownApiRule], }), ) expect(problems).toEqual([]) }) })