|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { highlightedOffenses, runLiquidCheck } from '../../test'; |
| 3 | +import { ValidStandardEventData } from './index'; |
| 4 | + |
| 5 | +describe('Module: ValidStandardEventData', () => { |
| 6 | + it('reports an offense when the context is not a supported value for products', async () => { |
| 7 | + const sourceCode = `{{ product | standard_event_data: 'view', context: 'homepage' }}`; |
| 8 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 9 | + |
| 10 | + expect(offenses).toHaveLength(1); |
| 11 | + expect(offenses[0].message).toBe( |
| 12 | + "Unsupported context 'homepage' for product. Valid values: page, search, collection, dialog, recommendation. The 'context' argument can also be omitted.", |
| 13 | + ); |
| 14 | + |
| 15 | + const highlights = highlightedOffenses({ 'file.liquid': sourceCode }, offenses); |
| 16 | + expect(highlights[0]).to.eql("'homepage'"); |
| 17 | + }); |
| 18 | + |
| 19 | + it('reports an offense when the context is not a supported value for carts', async () => { |
| 20 | + const sourceCode = `{{ cart | standard_event_data: 'view', context: 'banner' }}`; |
| 21 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 22 | + |
| 23 | + expect(offenses).toHaveLength(1); |
| 24 | + expect(offenses[0].message).toBe( |
| 25 | + "Unsupported context 'banner' for cart. Valid values: page, dialog. The 'context' argument can also be omitted.", |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + it('reports an offense when the context is valid for products but the input is a cart', async () => { |
| 30 | + const sourceCode = `{{ cart | standard_event_data: 'view', context: 'recommendation' }}`; |
| 31 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 32 | + |
| 33 | + expect(offenses).toHaveLength(1); |
| 34 | + expect(offenses[0].message).toBe( |
| 35 | + "Unsupported context 'recommendation' for cart. Valid values: page, dialog. The 'context' argument can also be omitted.", |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('does not report an offense on supported product context values', async () => { |
| 40 | + const contexts = ['page', 'search', 'collection', 'dialog', 'recommendation']; |
| 41 | + |
| 42 | + for (const context of contexts) { |
| 43 | + const sourceCode = `{{ product | standard_event_data: 'view', context: '${context}' }}`; |
| 44 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 45 | + |
| 46 | + expect(offenses, `expected '${context}' to be a valid product context`).toHaveLength(0); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not report an offense on supported cart context values', async () => { |
| 51 | + const contexts = ['page', 'dialog']; |
| 52 | + |
| 53 | + for (const context of contexts) { |
| 54 | + const sourceCode = `{{ cart | standard_event_data: 'view', context: '${context}' }}`; |
| 55 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 56 | + |
| 57 | + expect(offenses, `expected '${context}' to be a valid cart context`).toHaveLength(0); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not report an offense on collections, whose context is ignored', async () => { |
| 62 | + const sourceCode = `{{ collection | standard_event_data: 'view', context: 'homepage' }}`; |
| 63 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 64 | + |
| 65 | + expect(offenses).toHaveLength(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it('falls back to the union of contexts when the input is not a known global', async () => { |
| 69 | + const sourceCode = `{{ line_item.product | standard_event_data: 'view', context: 'recommendation' }}`; |
| 70 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 71 | + |
| 72 | + expect(offenses).toHaveLength(0); |
| 73 | + }); |
| 74 | + |
| 75 | + it('falls back to the union of contexts when the filter input is chained', async () => { |
| 76 | + const sourceCode = `{{ cart | default: other_cart | standard_event_data: 'view', context: 'recommendation' }}`; |
| 77 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 78 | + |
| 79 | + expect(offenses).toHaveLength(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it('reports an offense when the context is outside the union for an unknown input', async () => { |
| 83 | + const sourceCode = `{{ line_item.product | standard_event_data: 'view', context: 'homepage' }}`; |
| 84 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 85 | + |
| 86 | + expect(offenses).toHaveLength(1); |
| 87 | + expect(offenses[0].message).toBe( |
| 88 | + "Unsupported context 'homepage'. Valid values: page, search, collection, dialog, recommendation. The 'context' argument can also be omitted.", |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not report an offense when the context is a variable', async () => { |
| 93 | + const sourceCode = `{{ product | standard_event_data: 'view', context: section.settings.context }}`; |
| 94 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 95 | + |
| 96 | + expect(offenses).toHaveLength(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it('reports an offense when the context is a non-string literal', async () => { |
| 100 | + const sourceCode = `{{ product | standard_event_data: 'view', context: 123 }}`; |
| 101 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 102 | + |
| 103 | + expect(offenses).toHaveLength(1); |
| 104 | + expect(offenses[0].message).toBe( |
| 105 | + "Unsupported context for product. Valid values: page, search, collection, dialog, recommendation. The 'context' argument can also be omitted.", |
| 106 | + ); |
| 107 | + |
| 108 | + const highlights = highlightedOffenses({ 'file.liquid': sourceCode }, offenses); |
| 109 | + expect(highlights[0]).to.eql('123'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('does not report an offense when the context argument is omitted', async () => { |
| 113 | + const sourceCode = `{{ collection | standard_event_data: 'view' }}`; |
| 114 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 115 | + |
| 116 | + expect(offenses).toHaveLength(0); |
| 117 | + }); |
| 118 | + |
| 119 | + it('reports an offense when the event type is not supported', async () => { |
| 120 | + const sourceCode = `{{ product | standard_event_data: 'click', context: 'page' }}`; |
| 121 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 122 | + |
| 123 | + expect(offenses).toHaveLength(1); |
| 124 | + expect(offenses[0].message).toBe( |
| 125 | + "Unsupported event type 'click'. The only supported event type is 'view'.", |
| 126 | + ); |
| 127 | + |
| 128 | + const highlights = highlightedOffenses({ 'file.liquid': sourceCode }, offenses); |
| 129 | + expect(highlights[0]).to.eql("'click'"); |
| 130 | + }); |
| 131 | + |
| 132 | + it('reports an offense when the event type is not supported on a collection', async () => { |
| 133 | + const sourceCode = `{{ collection | standard_event_data: 'click' }}`; |
| 134 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 135 | + |
| 136 | + expect(offenses).toHaveLength(1); |
| 137 | + expect(offenses[0].message).toBe( |
| 138 | + "Unsupported event type 'click'. The only supported event type is 'view'.", |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('does not report an offense when the event type is a variable', async () => { |
| 143 | + const sourceCode = `{{ product | standard_event_data: event_type }}`; |
| 144 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 145 | + |
| 146 | + expect(offenses).toHaveLength(0); |
| 147 | + }); |
| 148 | + |
| 149 | + it('reports an offense when the event type is a non-string literal', async () => { |
| 150 | + const sourceCode = `{{ product | standard_event_data: 123 }}`; |
| 151 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 152 | + |
| 153 | + expect(offenses).toHaveLength(1); |
| 154 | + expect(offenses[0].message).toBe( |
| 155 | + "Unsupported event type. The only supported event type is 'view'.", |
| 156 | + ); |
| 157 | + |
| 158 | + const highlights = highlightedOffenses({ 'file.liquid': sourceCode }, offenses); |
| 159 | + expect(highlights[0]).to.eql('123'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('reports an offense when the event type is a boolean literal', async () => { |
| 163 | + const sourceCode = `{{ product | standard_event_data: true }}`; |
| 164 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 165 | + |
| 166 | + expect(offenses).toHaveLength(1); |
| 167 | + expect(offenses[0].message).toBe( |
| 168 | + "Unsupported event type. The only supported event type is 'view'.", |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + it('reports both offenses when the event type and the context are invalid', async () => { |
| 173 | + const sourceCode = `{{ product | standard_event_data: 'click', context: 'homepage' }}`; |
| 174 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 175 | + |
| 176 | + expect(offenses).toHaveLength(2); |
| 177 | + }); |
| 178 | + |
| 179 | + it('does not report an offense on other filters', async () => { |
| 180 | + const sourceCode = `{{ product | json }}{{ 'homepage' | append: 'view' }}`; |
| 181 | + const offenses = await runLiquidCheck(ValidStandardEventData, sourceCode); |
| 182 | + |
| 183 | + expect(offenses).toHaveLength(0); |
| 184 | + }); |
| 185 | +}); |
0 commit comments