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