-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.tsx
106 lines (88 loc) · 2.56 KB
/
mod_test.tsx
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
// @deno-types="https://deno.land/x/types/react/v16.13.1/react.d.ts"
import React from 'https://dev.jspm.io/react@16.13.1'
import { assertEquals, assertStrictEq } from 'https://deno.land/std@0.55.0/testing/asserts.ts'
import { spy } from 'https://raw.githubusercontent.com/udibo/mock/v0.3.0/spy.ts'
import { stub } from 'https://raw.githubusercontent.com/udibo/mock/v0.3.0/stub.ts'
import { Renderer } from './renderer.tsx'
import { DexrApp } from './mod.ts'
import { Router } from 'https://deno.land/x/oak@v5.1.0/router.ts'
import { Application, ListenOptions } from 'https://deno.land/x/oak@v5.1.0/application.ts'
import {
serve as denoServe,
Server,
ServerRequest,
} from 'https://deno.land/x/oak@v5.1.0/deps.ts'
let serverRequestStack: ServerRequest[] = []
const teardown = () => {
serverRequestStack = []
}
class MockServer {
close(): void {
}
async* [Symbol.asyncIterator]() {
for await (const request of serverRequestStack) {
yield request
}
}
}
const serve: typeof denoServe = function (
addr: string | ListenOptions,
): Server {
return new MockServer() as Server
}
type Context = {
request: {
url: {
searchParams: Array<[string, any]>
}
}
response: {
headers?: Headers
body?: string
}
}
Deno.test('useRenderer, addPage logic', async () => {
const router = new Router()
const spyRouterGet = spy(router, 'get')
const renderer = new Renderer()
const render = stub(renderer, 'render')
const renderResult = '<p>test success!</p>'
render.returns = [renderResult]
const dexr = new DexrApp({
router: router,
})
await dexr
.useRenderer(renderer)
.addPage('test', '/example/hello-world/App.tsx')
assertEquals(spyRouterGet.calls.length, 1)
const { args, self } = spyRouterGet.calls[0]
assertEquals(self, router)
const [route, handler] = args
assertEquals(route, 'test')
const dummyContext: Context = {
request: {
url: {
searchParams: []
}
},
response: {}
}
handler(dummyContext)
assertEquals(dummyContext.response.body, renderResult)
spyRouterGet.restore()
})
Deno.test('run logic', async () => {
const application = new Application({ serve })
const router = new Router()
const spyUse = spy(application, 'use')
const spyListen = spy(application, 'listen')
const dexr = new DexrApp({ application, router })
await dexr.run()
assertEquals(spyUse.calls.length, 1)
assertStrictEq(spyUse.calls[0].args[0].router, router)
assertEquals(spyListen.calls.length, 1)
assertEquals(spyListen.calls[0].args[0], {
port: 8000,
})
teardown()
})