Skip to content

Commit ba7a772

Browse files
committed
move hooks spec to tests folder
1 parent 5a02cc5 commit ba7a772

File tree

2 files changed

+26
-45
lines changed

2 files changed

+26
-45
lines changed

test/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ExecuteOptions = Partial<{
1111
fetch: Fetch
1212
modifier: ModifierHook
1313
willThrow: boolean
14+
hooks: Hooks
1415
}>
1516

1617
export type Fetch = (url: string, opts: RequestInit) => MaybePromise<Response>
@@ -54,6 +55,7 @@ export async function execute(
5455
body: await res.text(),
5556
}
5657
},
58+
...options.hooks,
5759
}
5860

5961
return executeModule('Home', inputs, hooks)

packages/get/src/hooks.spec.ts renamed to test/hooks.spec.ts

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
SliceHook,
99
} from '@getlang/lib'
1010
import { invariant } from '@getlang/lib'
11-
import { execute } from './index.js'
11+
import { execute } from './helpers.js'
1212

1313
describe('hook', () => {
1414
test('on request', async () => {
@@ -19,29 +19,27 @@ describe('hook', () => {
1919
extract -> h1
2020
`
2121

22-
const requestHook = mock<RequestHook>(async () => ({
22+
const request = mock<RequestHook>(async () => ({
2323
status: 200,
2424
headers: new Headers({ 'content-type': 'text/html' }),
2525
body: '<!doctype html><h1>test</h1>',
2626
}))
2727

28-
const result = await execute(src, {}, { request: requestHook })
28+
const result = await execute(src, {}, { hooks: { request } })
2929

30-
expect(requestHook).toHaveBeenCalledWith('http://get.com/', {
30+
expect(request).toHaveServed('http://get.com/', {
3131
method: 'GET',
32-
headers: expect.headers(
33-
new globalThis.Headers({
34-
Accept: 'text/html',
35-
}),
36-
),
32+
headers: new Headers({
33+
Accept: 'text/html',
34+
}),
3735
})
3836
expect(result).toEqual('test')
3937
})
4038

4139
test('on slice', async () => {
42-
const sliceHook = mock<SliceHook>(() => 3)
43-
const result = await execute('extract `1 + 2`', {}, { slice: sliceHook })
44-
expect(sliceHook).toHaveBeenCalledWith('return 1 + 2;;', undefined)
40+
const slice = mock<SliceHook>(() => 3)
41+
const result = await execute('extract `1 + 2`', {}, { hooks: { slice } })
42+
expect(slice).toHaveBeenCalledWith('return 1 + 2;;', undefined)
4543
expect(result).toEqual(3)
4644
})
4745

@@ -60,17 +58,16 @@ describe('hook', () => {
6058
}
6159
}
6260
`,
63-
}
64-
65-
const src = `
66-
set inputA = |"foo"|
61+
Home: `
62+
set inputA = |"foo"|
6763
68-
extract {
69-
topValue: @Top({ $inputA }) -> value
70-
midValue: @Mid -> value
71-
botValue: |"bot"|
72-
}
73-
`
64+
extract {
65+
topValue: @Top({ $inputA }) -> value
66+
midValue: @Mid -> value
67+
botValue: |"bot"|
68+
}
69+
`,
70+
}
7471

7572
const hooks: Hooks = {
7673
import: mock<ImportHook>(async (module: string) => {
@@ -81,7 +78,7 @@ describe('hook', () => {
8178
call: mock<CallHook>(() => {}),
8279
extract: mock<ExtractHook>(() => {}),
8380
}
84-
const result = await execute(src, {}, hooks)
81+
const result = await execute(modules, {}, { hooks })
8582

8683
expect(result).toEqual({
8784
topValue: 'top::foo',
@@ -92,9 +89,10 @@ describe('hook', () => {
9289
botValue: 'bot',
9390
})
9491

95-
expect(hooks.import).toHaveBeenCalledTimes(2)
96-
expect(hooks.import).toHaveBeenNthCalledWith(1, 'Top')
97-
expect(hooks.import).toHaveBeenNthCalledWith(2, 'Mid')
92+
expect(hooks.import).toHaveBeenCalledTimes(3)
93+
expect(hooks.import).toHaveBeenNthCalledWith(1, 'Home')
94+
expect(hooks.import).toHaveBeenNthCalledWith(2, 'Top')
95+
expect(hooks.import).toHaveBeenNthCalledWith(3, 'Mid')
9896

9997
expect(hooks.call).toHaveBeenCalledTimes(3)
10098
expect(hooks.call).toHaveBeenNthCalledWith(1, 'Top', { inputA: 'foo' })
@@ -127,22 +125,3 @@ describe('hook', () => {
127125
)
128126
})
129127
})
130-
131-
expect.extend({
132-
headers(received: unknown, expected: Headers) {
133-
if (!(received instanceof Headers)) {
134-
return {
135-
message: () => 'expected headers object',
136-
pass: false,
137-
}
138-
}
139-
140-
const pass = this.equals(
141-
Object.fromEntries(received as any),
142-
Object.fromEntries(expected as any),
143-
)
144-
145-
const message = () => 'todo'
146-
return { pass, message }
147-
},
148-
})

0 commit comments

Comments
 (0)