|
| 1 | +import path from 'node:path' |
| 2 | + |
| 3 | +import {describe, expect, test} from '@jest/globals' |
| 4 | + |
| 5 | +import {findQueriesInSource} from '../findQueriesInSource' |
| 6 | + |
| 7 | +describe('findQueries', () => { |
| 8 | + describe('should find queries in source', () => { |
| 9 | + test('plain string', () => { |
| 10 | + const source = ` |
| 11 | + import { groq } from "groq"; |
| 12 | + const postQuery = groq\`*[_type == "author"]\` |
| 13 | + const res = sanity.fetch(postQuery); |
| 14 | + ` |
| 15 | + |
| 16 | + const queries = findQueriesInSource(source, 'test.ts') |
| 17 | + const queryResult = queries[0] |
| 18 | + |
| 19 | + expect(queryResult?.result).toEqual('*[_type == "author"]') |
| 20 | + }) |
| 21 | + |
| 22 | + test('with variables', () => { |
| 23 | + const source = ` |
| 24 | + import { groq } from "groq"; |
| 25 | + const type = "author"; |
| 26 | + const authorQuery = groq\`*[_type == "\${type}"]\` |
| 27 | + const res = sanity.fetch(authorQuery); |
| 28 | + ` |
| 29 | + |
| 30 | + const queries = findQueriesInSource(source, 'test.ts') |
| 31 | + const queryResult = queries[0] |
| 32 | + |
| 33 | + expect(queryResult?.result).toEqual('*[_type == "author"]') |
| 34 | + }) |
| 35 | + |
| 36 | + test('with function', () => { |
| 37 | + const source = ` |
| 38 | + import { groq } from "groq"; |
| 39 | + const getType = () => () => () => "author"; |
| 40 | + const query = groq\`*[_type == "\${getType()()()}"]\` |
| 41 | + const res = sanity.fetch(query); |
| 42 | + ` |
| 43 | + |
| 44 | + const queries = findQueriesInSource(source, 'test.ts') |
| 45 | + |
| 46 | + const queryResult = queries[0] |
| 47 | + |
| 48 | + expect(queryResult?.result).toEqual('*[_type == "author"]') |
| 49 | + }) |
| 50 | + |
| 51 | + test('with block comment', () => { |
| 52 | + const source = ` |
| 53 | + import { groq } from "groq"; |
| 54 | + const type = "author"; |
| 55 | + const query = /* groq */ groq\`*[_type == "\${type}"]\`; |
| 56 | + const res = sanity.fetch(query); |
| 57 | + ` |
| 58 | + |
| 59 | + const queries = findQueriesInSource(source, 'test.ts') |
| 60 | + const queryResult = queries[0] |
| 61 | + |
| 62 | + expect(queryResult?.result).toEqual('*[_type == "author"]') |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + test('should not find inline queries in source', () => { |
| 67 | + const source = ` |
| 68 | + import { groq } from "groq"; |
| 69 | + const res = sanity.fetch(groq\`*[_type == "author"]\`); |
| 70 | + ` |
| 71 | + |
| 72 | + const queries = findQueriesInSource(source, 'test.ts') |
| 73 | + |
| 74 | + expect(queries.length).toBe(0) |
| 75 | + }) |
| 76 | + |
| 77 | + test("should name queries with 'Result' at the end", () => { |
| 78 | + const source = ` |
| 79 | + import { groq } from "groq"; |
| 80 | + const postQuery = groq\`*[_type == "author"]\` |
| 81 | + const res = sanity.fetch(postQueryResult); |
| 82 | + ` |
| 83 | + |
| 84 | + const queries = findQueriesInSource(source, 'test.ts') |
| 85 | + const queryResult = queries[0] |
| 86 | + |
| 87 | + expect(queryResult?.name.substr(-6)).toBe('Result') |
| 88 | + }) |
| 89 | + |
| 90 | + test('should import', () => { |
| 91 | + const source = ` |
| 92 | + import { groq } from "groq"; |
| 93 | + import {foo} from "./fixtures/exportVar"; |
| 94 | + const postQuery = groq\`*[_type == "\${foo}"]\` |
| 95 | + const res = sanity.fetch(postQueryResult); |
| 96 | + ` |
| 97 | + |
| 98 | + const resolver: NodeJS.RequireResolve = (id) => { |
| 99 | + if (id === 'foo') { |
| 100 | + return path.resolve(__dirname, 'fixtures', 'exportVar') |
| 101 | + } |
| 102 | + return require.resolve(id) |
| 103 | + } |
| 104 | + resolver.paths = (request: string): string[] | null => { |
| 105 | + return require.resolve.paths(request) |
| 106 | + } |
| 107 | + |
| 108 | + const queries = findQueriesInSource(source, 'test.ts', undefined, resolver) |
| 109 | + const queryResult = queries[0] |
| 110 | + |
| 111 | + expect(queryResult?.name.substr(-6)).toBe('Result') |
| 112 | + }) |
| 113 | +}) |
0 commit comments