|
| 1 | +import { |
| 2 | + displayTypeRequiresSource, |
| 3 | + isBuilderSavedChartConfig, |
| 4 | + isRawSqlSavedChartConfig, |
| 5 | +} from '@/guards'; |
| 6 | +import { DisplayType } from '@/types'; |
| 7 | + |
| 8 | +describe('isRawSqlSavedChartConfig', () => { |
| 9 | + it('returns true when configType is "sql"', () => { |
| 10 | + expect( |
| 11 | + isRawSqlSavedChartConfig({ |
| 12 | + configType: 'sql', |
| 13 | + displayType: DisplayType.Table, |
| 14 | + connection: 'conn-id', |
| 15 | + sqlTemplate: 'SELECT 1', |
| 16 | + } as any), |
| 17 | + ).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it('returns false for a builder config with no configType', () => { |
| 21 | + expect( |
| 22 | + isRawSqlSavedChartConfig({ |
| 23 | + displayType: DisplayType.Line, |
| 24 | + source: 'src-id', |
| 25 | + select: [], |
| 26 | + where: '', |
| 27 | + } as any), |
| 28 | + ).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns false for a markdown config', () => { |
| 32 | + expect( |
| 33 | + isRawSqlSavedChartConfig({ |
| 34 | + displayType: DisplayType.Markdown, |
| 35 | + markdown: '# Hello', |
| 36 | + source: '', |
| 37 | + where: '', |
| 38 | + select: [], |
| 39 | + } as any), |
| 40 | + ).toBe(false); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe('isBuilderSavedChartConfig', () => { |
| 45 | + it('returns true for a standard builder config', () => { |
| 46 | + expect( |
| 47 | + isBuilderSavedChartConfig({ |
| 48 | + displayType: DisplayType.Line, |
| 49 | + source: 'src-id', |
| 50 | + select: [], |
| 51 | + where: '', |
| 52 | + } as any), |
| 53 | + ).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns false for a raw SQL config', () => { |
| 57 | + expect( |
| 58 | + isBuilderSavedChartConfig({ |
| 59 | + configType: 'sql', |
| 60 | + displayType: DisplayType.Table, |
| 61 | + connection: 'conn-id', |
| 62 | + sqlTemplate: 'SELECT 1', |
| 63 | + } as any), |
| 64 | + ).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns true for a markdown tile config', () => { |
| 68 | + // Markdown tiles are stored as BuilderSavedChartConfig with source: '' |
| 69 | + // because the type requires a source field. displayTypeRequiresSource() |
| 70 | + // is the canonical guard for excluding sourceless display types from the |
| 71 | + // "source unset" check -- see DBDashboardPage isSourceUnset. |
| 72 | + expect( |
| 73 | + isBuilderSavedChartConfig({ |
| 74 | + displayType: DisplayType.Markdown, |
| 75 | + markdown: '# Hello', |
| 76 | + source: '', |
| 77 | + where: '', |
| 78 | + select: [], |
| 79 | + } as any), |
| 80 | + ).toBe(true); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('displayTypeRequiresSource', () => { |
| 85 | + it('returns false for Markdown', () => { |
| 86 | + expect(displayTypeRequiresSource(DisplayType.Markdown)).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns true for Line', () => { |
| 90 | + expect(displayTypeRequiresSource(DisplayType.Line)).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns true for Table', () => { |
| 94 | + expect(displayTypeRequiresSource(DisplayType.Table)).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns true for Search', () => { |
| 98 | + expect(displayTypeRequiresSource(DisplayType.Search)).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns true for StackedBar', () => { |
| 102 | + expect(displayTypeRequiresSource(DisplayType.StackedBar)).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns true for Pie', () => { |
| 106 | + expect(displayTypeRequiresSource(DisplayType.Pie)).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns true for undefined', () => { |
| 110 | + expect(displayTypeRequiresSource(undefined)).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('isSourceUnset pattern', () => { |
| 114 | + // These tests exercise the production isSourceUnset condition used in |
| 115 | + // DBDashboardPage via the real displayTypeRequiresSource export. |
| 116 | + function isSourceUnset(config: any): boolean { |
| 117 | + return ( |
| 118 | + !!config && |
| 119 | + isBuilderSavedChartConfig(config) && |
| 120 | + displayTypeRequiresSource(config.displayType) && |
| 121 | + !config.source |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + it('does not fire for markdown tiles with empty source', () => { |
| 126 | + expect( |
| 127 | + isSourceUnset({ |
| 128 | + displayType: DisplayType.Markdown, |
| 129 | + markdown: '# Hello', |
| 130 | + source: '', // stored as '' by convertToInternalTileConfig |
| 131 | + where: '', |
| 132 | + select: [], |
| 133 | + }), |
| 134 | + ).toBe(false); |
| 135 | + }); |
| 136 | + |
| 137 | + it('fires for builder tiles with empty source', () => { |
| 138 | + expect( |
| 139 | + isSourceUnset({ |
| 140 | + displayType: DisplayType.Line, |
| 141 | + source: '', |
| 142 | + select: [], |
| 143 | + where: '', |
| 144 | + }), |
| 145 | + ).toBe(true); |
| 146 | + }); |
| 147 | + |
| 148 | + it('does not fire for builder tiles with a real source', () => { |
| 149 | + expect( |
| 150 | + isSourceUnset({ |
| 151 | + displayType: DisplayType.Line, |
| 152 | + source: 'abc123', |
| 153 | + select: [], |
| 154 | + where: '', |
| 155 | + }), |
| 156 | + ).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('does not fire for raw SQL tiles', () => { |
| 160 | + expect( |
| 161 | + isSourceUnset({ |
| 162 | + configType: 'sql', |
| 163 | + displayType: DisplayType.Table, |
| 164 | + connection: 'conn-id', |
| 165 | + sqlTemplate: 'SELECT 1', |
| 166 | + }), |
| 167 | + ).toBe(false); |
| 168 | + }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments