-
Notifications
You must be signed in to change notification settings - Fork 11
/
gatsby-node.test.js
213 lines (186 loc) · 7.3 KB
/
gatsby-node.test.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const nock = require('nock')
const fs = require('fs-extra')
const { createResolvers } = require('./gatsby-node')
const registeredResolvers = new Map()
const actualCacheMap = new Map()
const cacheDir = __dirname + '/.cache'
fs.ensureDirSync(cacheDir)
const cache = {
get: jest.fn((key) => actualCacheMap.get(key)),
set: jest.fn((key, value) => actualCacheMap.set(key, value)),
directory: cacheDir,
actualMap: actualCacheMap
}
const createResolversMock = jest.fn((resolvers) => {
Object.keys(resolvers).forEach((resolverName) => {
registeredResolvers.set(resolverName, resolvers[resolverName])
})
})
const store = {
getState: jest.fn(() => {
return { program: { directory: process.cwd() }, status: {} }
})
}
const reporter = { info: jest.fn(), panic: jest.fn() }
const fixturePath = __dirname + '/fixtures/gatsby-monogram.svg'
const pixelatedFixturePath =
__dirname + '/fixtures/gatsby-logo-with-pixel-data.svg'
const fileFixture = {
absolutePath: fixturePath,
internal: {
mediaType: 'image/svg+xml'
}
}
const contentfulAssetFixture = {
file: {
url: '//localhost/mocked.svg',
contentType: 'image/svg+xml'
},
updatedAt: '123'
}
describe('general', () => {
beforeEach(() => {
createResolversMock.mockClear()
registeredResolvers.clear()
reporter.panic.mockClear()
})
test('create resolver', async () => {
createResolvers({
cache,
createResolvers: createResolversMock,
store,
reporter
})
expect(registeredResolvers.size).toBe(2)
expect(registeredResolvers.has('ContentfulAsset')).toBe(true)
const resolverData = registeredResolvers.get('ContentfulAsset')
expect(resolverData.svg).toBeTruthy()
expect(resolverData.svg.type).toBe('InlineSvg')
})
test('removes pixelated data', async () => {
createResolvers({
cache,
createResolvers: createResolversMock,
store,
reporter
})
const resolverData = registeredResolvers.get('File')
const pixelatedFixture = JSON.parse(JSON.stringify(fileFixture))
pixelatedFixture.absolutePath = pixelatedFixturePath
const result = await resolverData.svg.resolve(pixelatedFixture)
expect(reporter.panic).not.toBeCalled()
expect(result.content).not.toContain('data:image/png')
})
})
describe('gatsby-source-filesystem', () => {
beforeEach(() => {
createResolversMock.mockClear()
registeredResolvers.clear()
reporter.panic.mockClear()
nock.cleanAll()
cache.actualMap.clear()
})
test('process File node', async () => {
createResolvers({
cache,
createResolvers: createResolversMock,
store,
reporter
})
const resolverData = registeredResolvers.get('File')
const result = await resolverData.svg.resolve(fileFixture)
expect(reporter.panic).not.toBeCalled()
expect(result.absolutePath).toContain('svg')
expect(result.absolutePath).toContain(__dirname)
expect(result.content).toBe(
`<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 28 28\"><circle cx=\"14\" cy=\"14\" r=\"14\" fill=\"#639\"/><path fill=\"#fff\" d=\"M6.2 21.8C4.1 19.7 3 16.9 3 14.2L13.9 25c-2.8-.1-5.6-1.1-7.7-3.2zm10.2 2.9L3.3 11.6C4.4 6.7 8.8 3 14 3c3.7 0 6.9 1.8 8.9 4.5l-1.5 1.3A9.23 9.23 0 0 0 14 5a9.1 9.1 0 0 0-8.5 6L17 22.5c2.9-1 5.1-3.5 5.8-6.5H18v-2h7c0 5.2-3.7 9.6-8.6 10.7z\"/></svg>`
)
expect(result.dataURI).toBe(
`data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 28 28'%3e%3ccircle cx='14' cy='14' r='14' fill='%23639'/%3e%3cpath fill='white' d='M6.2 21.8C4.1 19.7 3 16.9 3 14.2L13.9 25c-2.8-.1-5.6-1.1-7.7-3.2zm10.2 2.9L3.3 11.6C4.4 6.7 8.8 3 14 3c3.7 0 6.9 1.8 8.9 4.5l-1.5 1.3A9.23 9.23 0 0 0 14 5a9.1 9.1 0 0 0-8.5 6L17 22.5c2.9-1 5.1-3.5 5.8-6.5H18v-2h7c0 5.2-3.7 9.6-8.6 10.7z'/%3e%3c/svg%3e`
)
expect(result.originalContent).toBe(
fs.readFileSync(__dirname + '/fixtures/gatsby-monogram.svg').toString()
)
expect(result.relativePath).toBe('fixtures/gatsby-monogram.svg')
})
test('skip non-svg File nodes', async () => {
createResolvers({
cache,
createResolvers: createResolversMock,
store,
reporter
})
const resolverData = registeredResolvers.get('File')
const noSvgFixture = JSON.parse(JSON.stringify(fileFixture))
noSvgFixture.internal.mediaType = 'no/svg'
const result = await resolverData.svg.resolve(noSvgFixture)
expect(result).toBe(null)
expect(reporter.panic).not.toHaveBeenCalled()
})
})
describe('gatsby-source-contentful', () => {
beforeEach(() => {
createResolversMock.mockClear()
registeredResolvers.clear()
reporter.panic.mockClear()
nock.cleanAll()
cache.actualMap.clear()
})
test('process Contentful Asset', async () => {
createResolvers({
cache,
createResolvers: createResolversMock,
store,
reporter
})
const scope = nock('https://localhost')
.get(`/mocked.svg`)
.replyWithFile(200, fixturePath, {
'Content-Type': 'image/svg+xml'
})
const resolverData = registeredResolvers.get('ContentfulAsset')
const result = await resolverData.svg.resolve(contentfulAssetFixture)
expect(reporter.panic).not.toBeCalled()
expect(scope.isDone()).toBeTruthy()
expect(result.absolutePath).toContain('svg')
expect(result.absolutePath).toContain(__dirname)
expect(result.content).toBe(
`<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 28 28\"><circle cx=\"14\" cy=\"14\" r=\"14\" fill=\"#639\"/><path fill=\"#fff\" d=\"M6.2 21.8C4.1 19.7 3 16.9 3 14.2L13.9 25c-2.8-.1-5.6-1.1-7.7-3.2zm10.2 2.9L3.3 11.6C4.4 6.7 8.8 3 14 3c3.7 0 6.9 1.8 8.9 4.5l-1.5 1.3A9.23 9.23 0 0 0 14 5a9.1 9.1 0 0 0-8.5 6L17 22.5c2.9-1 5.1-3.5 5.8-6.5H18v-2h7c0 5.2-3.7 9.6-8.6 10.7z\"/></svg>`
)
expect(result.dataURI).toBe(
`data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 28 28'%3e%3ccircle cx='14' cy='14' r='14' fill='%23639'/%3e%3cpath fill='white' d='M6.2 21.8C4.1 19.7 3 16.9 3 14.2L13.9 25c-2.8-.1-5.6-1.1-7.7-3.2zm10.2 2.9L3.3 11.6C4.4 6.7 8.8 3 14 3c3.7 0 6.9 1.8 8.9 4.5l-1.5 1.3A9.23 9.23 0 0 0 14 5a9.1 9.1 0 0 0-8.5 6L17 22.5c2.9-1 5.1-3.5 5.8-6.5H18v-2h7c0 5.2-3.7 9.6-8.6 10.7z'/%3e%3c/svg%3e`
)
expect(result.originalContent).toBe(
fs.readFileSync(__dirname + '/fixtures/gatsby-monogram.svg').toString()
)
expect(result.relativePath).toContain('mocked.svg')
})
test('skip non-svg Contentful assets', async () => {
createResolvers({
cache,
createResolvers: createResolversMock,
store,
reporter
})
const resolverData = registeredResolvers.get('ContentfulAsset')
const noSvgFixture = JSON.parse(JSON.stringify(contentfulAssetFixture))
noSvgFixture.file.contentType = 'no/svg'
const result = await resolverData.svg.resolve(noSvgFixture)
expect(result).toBe(null)
expect(reporter.panic).not.toHaveBeenCalled()
})
test('skip empty Contentful assets', async () => {
createResolvers({
cache,
createResolvers: createResolversMock,
store,
reporter
})
const resolverData = registeredResolvers.get('ContentfulAsset')
const noFileFixture = JSON.parse(JSON.stringify(contentfulAssetFixture))
delete noFileFixture.file.url
const result = await resolverData.svg.resolve(noFileFixture)
expect(result).toBe(null)
expect(reporter.panic).not.toHaveBeenCalled()
})
})