|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { DocumentManager } from '../../documents'; |
| 3 | +import { createJSONDocumentLinksVisitor } from './DocumentLinksProvider'; |
| 4 | +import { toJSONAST, visit } from '@shopify/theme-check-common'; |
| 5 | +import { URI } from 'vscode-uri'; |
| 6 | + |
| 7 | +describe('JSON Document Links in Schema Tags', () => { |
| 8 | + let documentManager: DocumentManager; |
| 9 | + let rootUri: string; |
| 10 | + let uriString: string; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + documentManager = new DocumentManager(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return document links for block types in schema tags', async () => { |
| 17 | + uriString = 'file:///path/to/sections/header.liquid'; |
| 18 | + rootUri = 'file:///path/to/project'; |
| 19 | + |
| 20 | + const schemaContent = ` |
| 21 | + { |
| 22 | + "name": "Header", |
| 23 | + "blocks": [ |
| 24 | + { "type": "logo" }, |
| 25 | + { "type": "navigation" } |
| 26 | + ] |
| 27 | + } |
| 28 | + `; |
| 29 | + |
| 30 | + const liquidContent = ` |
| 31 | + <div>Header content</div> |
| 32 | + |
| 33 | + {% schema %} |
| 34 | + ${schemaContent} |
| 35 | + {% endschema %} |
| 36 | + `; |
| 37 | + |
| 38 | + documentManager.open(uriString, liquidContent, 1); |
| 39 | + |
| 40 | + const textDocument = documentManager.get(uriString)!.textDocument; |
| 41 | + const schemaOffset = liquidContent.indexOf(schemaContent); |
| 42 | + |
| 43 | + const visitor = createJSONDocumentLinksVisitor(textDocument, URI.parse(rootUri), schemaOffset); |
| 44 | + const ast = toJSONAST(schemaContent); |
| 45 | + |
| 46 | + if (ast instanceof Error) throw ast; |
| 47 | + const result = visit(ast, visitor); |
| 48 | + |
| 49 | + expect(result).toHaveLength(2); |
| 50 | + expect(result[0].target).toBe('file:///path/to/project/blocks/logo.liquid'); |
| 51 | + expect(result[1].target).toBe('file:///path/to/project/blocks/navigation.liquid'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle section types in schema tags', async () => { |
| 55 | + uriString = 'file:///path/to/sections/main-collection.liquid'; |
| 56 | + rootUri = 'file:///path/to/project'; |
| 57 | + |
| 58 | + const schemaContent = ` |
| 59 | + { |
| 60 | + "name": "Collection", |
| 61 | + "blocks": [ |
| 62 | + { |
| 63 | + "type": "text", |
| 64 | + "settings": [] |
| 65 | + } |
| 66 | + ], |
| 67 | + "presets": [ |
| 68 | + { |
| 69 | + "name": "Default", |
| 70 | + "blocks": [ |
| 71 | + { "type": "text" } |
| 72 | + ] |
| 73 | + } |
| 74 | + ] |
| 75 | + } |
| 76 | + `; |
| 77 | + |
| 78 | + const liquidContent = ` |
| 79 | + <div>Collection content</div> |
| 80 | + |
| 81 | + {% schema %} |
| 82 | + ${schemaContent} |
| 83 | + {% endschema %} |
| 84 | + `; |
| 85 | + |
| 86 | + documentManager.open(uriString, liquidContent, 1); |
| 87 | + |
| 88 | + const textDocument = documentManager.get(uriString)!.textDocument; |
| 89 | + const schemaOffset = liquidContent.indexOf(schemaContent); |
| 90 | + |
| 91 | + const visitor = createJSONDocumentLinksVisitor(textDocument, URI.parse(rootUri), schemaOffset); |
| 92 | + const ast = toJSONAST(schemaContent); |
| 93 | + |
| 94 | + if (ast instanceof Error) throw ast; |
| 95 | + const result = visit(ast, visitor); |
| 96 | + |
| 97 | + // Should find both block type references (in blocks array and in presets) |
| 98 | + expect(result).toHaveLength(2); |
| 99 | + expect(result[0].target).toBe('file:///path/to/project/blocks/text.liquid'); |
| 100 | + expect(result[1].target).toBe('file:///path/to/project/blocks/text.liquid'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not create links for special block types in schema tags', async () => { |
| 104 | + uriString = 'file:///path/to/sections/app-blocks.liquid'; |
| 105 | + rootUri = 'file:///path/to/project'; |
| 106 | + |
| 107 | + const schemaContent = ` |
| 108 | + { |
| 109 | + "name": "App Blocks", |
| 110 | + "blocks": [ |
| 111 | + { "type": "@app" }, |
| 112 | + { "type": "@theme" }, |
| 113 | + { "type": "custom-block" } |
| 114 | + ] |
| 115 | + } |
| 116 | + `; |
| 117 | + |
| 118 | + const liquidContent = ` |
| 119 | + <div>App blocks section</div> |
| 120 | + |
| 121 | + {% schema %} |
| 122 | + ${schemaContent} |
| 123 | + {% endschema %} |
| 124 | + `; |
| 125 | + |
| 126 | + documentManager.open(uriString, liquidContent, 1); |
| 127 | + |
| 128 | + const textDocument = documentManager.get(uriString)!.textDocument; |
| 129 | + const schemaOffset = liquidContent.indexOf(schemaContent); |
| 130 | + |
| 131 | + const visitor = createJSONDocumentLinksVisitor(textDocument, URI.parse(rootUri), schemaOffset); |
| 132 | + const ast = toJSONAST(schemaContent); |
| 133 | + |
| 134 | + if (ast instanceof Error) throw ast; |
| 135 | + const result = visit(ast, visitor); |
| 136 | + |
| 137 | + // Should only create link for custom-block, not @app or @theme |
| 138 | + expect(result).toHaveLength(1); |
| 139 | + expect(result[0].target).toBe('file:///path/to/project/blocks/custom-block.liquid'); |
| 140 | + }); |
| 141 | +}); |
0 commit comments