|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { runLiquidCheck } from '../../test'; |
| 3 | +import { ValidRenderSnippetParamTypes } from '.'; |
| 4 | +import { MockFileSystem } from '../../test'; |
| 5 | +import { SupportedParamTypes } from '../../liquid-doc/utils'; |
| 6 | + |
| 7 | +describe('Module: ValidRenderSnippetParamTypes', () => { |
| 8 | + describe('type validation', () => { |
| 9 | + const typeTests = [ |
| 10 | + { |
| 11 | + type: 'string', |
| 12 | + validValues: ["'hello'", "''", 'product'], |
| 13 | + invalidValues: [ |
| 14 | + { value: '123', actualType: SupportedParamTypes.Number }, |
| 15 | + { value: 'true', actualType: SupportedParamTypes.Boolean }, |
| 16 | + ], |
| 17 | + }, |
| 18 | + { |
| 19 | + type: 'number', |
| 20 | + validValues: ['0', '123', '-1', 'product'], |
| 21 | + invalidValues: [ |
| 22 | + { value: "'hello'", actualType: SupportedParamTypes.String }, |
| 23 | + { value: 'true', actualType: SupportedParamTypes.Boolean }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + { |
| 27 | + type: 'boolean', |
| 28 | + validValues: ['true', 'false', 'nil', 'empty', 'product'], |
| 29 | + invalidValues: [ |
| 30 | + { value: "'hello'", actualType: SupportedParamTypes.String }, |
| 31 | + { value: '123', actualType: SupportedParamTypes.Number }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + { |
| 35 | + type: 'object', |
| 36 | + validValues: ['product', '(1..3)'], |
| 37 | + invalidValues: [ |
| 38 | + { value: "'hello'", actualType: SupportedParamTypes.String }, |
| 39 | + { value: '123', actualType: SupportedParamTypes.Number }, |
| 40 | + { value: 'true', actualType: SupportedParamTypes.Boolean }, |
| 41 | + { value: 'empty', actualType: SupportedParamTypes.Boolean }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + ]; |
| 45 | + |
| 46 | + for (const test of typeTests) { |
| 47 | + describe(`${test.type} validation`, () => { |
| 48 | + const makeSnippet = (type: string) => ` |
| 49 | + {% doc %} |
| 50 | + @param {${type}} param - Description |
| 51 | + {% enddoc %} |
| 52 | + <div>{{ param }}</div> |
| 53 | + `; |
| 54 | + |
| 55 | + test.validValues.forEach((value) => { |
| 56 | + it(`should accept ${value} for ${test.type}`, async () => { |
| 57 | + const fs = new MockFileSystem({ |
| 58 | + 'snippets/card.liquid': makeSnippet(test.type), |
| 59 | + }); |
| 60 | + |
| 61 | + const sourceCode = `{% render 'card', param: ${value} %}`; |
| 62 | + const offenses = await runLiquidCheck( |
| 63 | + ValidRenderSnippetParamTypes, |
| 64 | + sourceCode, |
| 65 | + undefined, |
| 66 | + { |
| 67 | + fs, |
| 68 | + }, |
| 69 | + ); |
| 70 | + expect(offenses).toHaveLength(0); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + test.invalidValues.forEach(({ value, actualType: expectedType }) => { |
| 75 | + it(`should reject ${value} for ${test.type}`, async () => { |
| 76 | + const fs = new MockFileSystem({ |
| 77 | + 'snippets/card.liquid': makeSnippet(test.type), |
| 78 | + }); |
| 79 | + |
| 80 | + const sourceCode = `{% render 'card', param: ${value} %}`; |
| 81 | + const offenses = await runLiquidCheck( |
| 82 | + ValidRenderSnippetParamTypes, |
| 83 | + sourceCode, |
| 84 | + undefined, |
| 85 | + { |
| 86 | + fs, |
| 87 | + }, |
| 88 | + ); |
| 89 | + expect(offenses).toHaveLength(1); |
| 90 | + expect(offenses[0].message).toBe( |
| 91 | + `Type mismatch for parameter 'param': expected ${test.type}, got ${expectedType}`, |
| 92 | + ); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + describe('edge cases', () => { |
| 100 | + it('should handle mixed case type annotations', async () => { |
| 101 | + const fs = new MockFileSystem({ |
| 102 | + 'snippets/card.liquid': ` |
| 103 | + {% doc %} |
| 104 | + @param {String} text - The text |
| 105 | + @param {NUMBER} count - The count |
| 106 | + @param {BOOLEAN} flag - The flag |
| 107 | + @param {Object} data - The data |
| 108 | + {% enddoc %} |
| 109 | + <div>{{ text }}{{ count }}{{ flag }}{{ data }}</div> |
| 110 | + `, |
| 111 | + }); |
| 112 | + |
| 113 | + const sourceCode = `{% render 'card', text: "hello", count: 5, flag: true, data: product %}`; |
| 114 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 115 | + fs, |
| 116 | + }); |
| 117 | + expect(offenses).toHaveLength(0); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should ignore variable lookups', async () => { |
| 121 | + const fs = new MockFileSystem({ |
| 122 | + 'snippets/card.liquid': ` |
| 123 | + {% doc %} |
| 124 | + @param {String} title - The title |
| 125 | + {% enddoc %} |
| 126 | + <div>{{ title }}</div> |
| 127 | + `, |
| 128 | + }); |
| 129 | + |
| 130 | + const sourceCode = `{% render 'card', title: product_title %}`; |
| 131 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 132 | + fs, |
| 133 | + }); |
| 134 | + expect(offenses).toHaveLength(0); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should not report when snippet has no doc comment', async () => { |
| 138 | + const fs = new MockFileSystem({ |
| 139 | + 'snippets/card.liquid': `<h1>This snippet has no doc comment</h1>`, |
| 140 | + }); |
| 141 | + |
| 142 | + const sourceCode = `{% render 'card', title: 123 %}`; |
| 143 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 144 | + fs, |
| 145 | + }); |
| 146 | + expect(offenses).toHaveLength(0); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should not report for unrelated parameters', async () => { |
| 150 | + const fs = new MockFileSystem({ |
| 151 | + 'snippets/card.liquid': ` |
| 152 | + {% doc %} |
| 153 | + @param {String} title - The title |
| 154 | + {% enddoc %} |
| 155 | + <div>{{ title }}</div> |
| 156 | + `, |
| 157 | + }); |
| 158 | + |
| 159 | + const sourceCode = `{% render 'card', title: "hello", unrelated: 123 %}`; |
| 160 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 161 | + fs, |
| 162 | + }); |
| 163 | + expect(offenses).toHaveLength(0); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments