@@ -3,126 +3,151 @@ import { createClientHeadWithContext, useDom } from '../../util'
3
3
4
4
describe ( 'titleTemplate' , ( ) => {
5
5
it ( 'string replace' , async ( ) => {
6
- const head = createClientHeadWithContext ( )
6
+ const dom = useDom ( )
7
+ const head = createClientHeadWithContext ( {
8
+ document : dom . window . document ,
9
+ } )
7
10
head . push ( {
8
11
titleTemplate : '%s - my template' ,
9
12
title : 'test' ,
10
13
} )
11
14
12
- const dom = useDom ( )
13
15
await renderDOMHead ( head , { document : dom . window . document } )
14
16
15
17
expect ( dom . window . document . title ) . toMatchInlineSnapshot (
16
18
`"test - my template"` ,
17
19
)
18
20
} )
19
21
it ( 'fn replace' , async ( ) => {
20
- const head = createClientHeadWithContext ( )
22
+ const dom = useDom ( )
23
+ const head = createClientHeadWithContext ( {
24
+ document : dom . window . document ,
25
+ } )
21
26
head . push ( {
22
27
titleTemplate : ( title ?: string ) => `${ title } - my template` ,
23
28
title : 'test' ,
24
29
} )
25
- const dom = useDom ( )
26
30
await renderDOMHead ( head , { document : dom . window . document } )
27
31
expect ( dom . window . document . title ) . toMatchInlineSnapshot (
28
32
`"test - my template"` ,
29
33
)
30
34
} )
31
35
it ( 'titleTemplate as title' , async ( ) => {
32
- const head = createClientHeadWithContext ( )
36
+ const dom = useDom ( )
37
+ const head = createClientHeadWithContext ( {
38
+ document : dom . window . document ,
39
+ } )
33
40
head . push ( {
34
41
titleTemplate : ( title ?: string ) => title ? `${ title } - Template` : 'Default Title' ,
35
42
title : null ,
36
43
} )
37
- const dom = useDom ( )
38
44
await renderDOMHead ( head , { document : dom . window . document } )
39
45
expect ( dom . window . document . title ) . toMatchInlineSnapshot (
40
46
`"Default Title"` ,
41
47
)
42
48
} )
43
49
// TODO convert to client
44
- // it('titleTemplate as title - update', async () => {
45
- // const head = createClientHeadWithContext()
46
- // head.push({
47
- // titleTemplate: (title?: string) => title ? `${title} - Template` : 'Default Title',
48
- // })
49
- // expect((await renderSSRHead(head)).headTags).toMatchInlineSnapshot(
50
- // `"<title>Default Title</title>"`,
51
- // )
52
- // const entry = head.push({
53
- // title: 'Hello world',
54
- // })
55
- // expect((await renderSSRHead(head)).headTags).toMatchInlineSnapshot(
56
- // `"<title>Hello world - Template</title>"`,
57
- // )
58
- // entry.dispose()
59
- // expect((await renderSSRHead(head)).headTags).toMatchInlineSnapshot(
60
- // `"<title>Default Title</title>"`,
61
- // )
62
- // })
63
- // it('reset title template', async () => {
64
- // const head = createClientHeadWithContext()
65
- // head.push({
66
- // titleTemplate: (title?: string) => title ? `${title} - Template` : 'Default Title',
67
- // })
68
- // head.push({
69
- // titleTemplate: null,
70
- // title: 'page title',
71
- // })
72
- // const { headTags } = await renderSSRHead(head)
73
- // expect(headTags).toMatchInlineSnapshot(
74
- // `"<title>page title</title>"`,
75
- // )
76
- // })
77
- //
78
- // it('nested title template', async () => {
79
- // const head = createClientHeadWithContext()
80
- // head.push({
81
- // titleTemplate: (title?: string) => title ? `${title} - Template` : 'Default Title',
82
- // })
83
- // head.push({
84
- // titleTemplate: null,
85
- // })
86
- // const { headTags } = await renderSSRHead(head)
87
- // expect(headTags).toMatchInlineSnapshot(
88
- // '""',
89
- // )
90
- // })
91
- //
92
- // it('null fn return', async () => {
93
- // const head = createClientHeadWithContext()
94
- // head.push({
95
- // titleTemplate: (title?: string) => title === 'test' ? null : `${title} - Template`,
96
- // title: 'test',
97
- // })
98
- // const { headTags } = await renderSSRHead(head)
99
- // expect(headTags).toMatchInlineSnapshot(
100
- // '""',
101
- // )
102
- // })
103
- //
104
- // it('empty title', async () => {
105
- // const head = createClientHeadWithContext()
106
- // head.push({
107
- // title: '',
108
- // })
109
- // const { headTags } = await renderSSRHead(head)
110
- // expect(headTags).toMatchInlineSnapshot(
111
- // `""`,
112
- // )
113
- // })
114
- //
115
- // it('replacing title with empty', async () => {
116
- // const head = createClientHeadWithContext()
117
- // head.push({
118
- // title: 'test',
119
- // })
120
- // head.push({
121
- // title: '',
122
- // })
123
- // const { headTags } = await renderSSRHead(head)
124
- // expect(headTags).toMatchInlineSnapshot(
125
- // `""`,
126
- // )
127
- // })
50
+ it ( 'titleTemplate as title - update' , async ( ) => {
51
+ const dom = useDom ( )
52
+ const head = createClientHeadWithContext ( {
53
+ document : dom . window . document ,
54
+ } )
55
+ head . push ( {
56
+ titleTemplate : ( title ?: string ) => title ? `${ title } - Template` : 'Default Title' ,
57
+ } )
58
+ await renderDOMHead ( head , { document : dom . window . document } )
59
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `"Default Title"` )
60
+ const entry = head . push ( {
61
+ title : 'Hello world' ,
62
+ } )
63
+ await renderDOMHead ( head , { document : dom . window . document } )
64
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `"Hello world - Template"` )
65
+ entry . dispose ( )
66
+ await renderDOMHead ( head , { document : dom . window . document } )
67
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `"Default Title"` )
68
+ } )
69
+ it ( 'reset title template' , async ( ) => {
70
+ const dom = useDom ( )
71
+ const head = createClientHeadWithContext ( {
72
+ document : dom . window . document ,
73
+ } )
74
+ head . push ( {
75
+ titleTemplate : ( title ?: string ) => title ? `${ title } - Template` : 'Default Title' ,
76
+ } )
77
+ head . push ( {
78
+ titleTemplate : null ,
79
+ title : 'page title' ,
80
+ } )
81
+ await renderDOMHead ( head , { document : dom . window . document } )
82
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `"page title"` )
83
+ } )
84
+
85
+ it ( 'nested title template' , async ( ) => {
86
+ const dom = useDom ( )
87
+ const head = createClientHeadWithContext ( {
88
+ document : dom . window . document ,
89
+ } )
90
+ head . push ( {
91
+ titleTemplate : ( title ?: string ) => title ? `${ title } - Template` : 'Default Title' ,
92
+ } )
93
+ head . push ( {
94
+ titleTemplate : null ,
95
+ } )
96
+ await renderDOMHead ( head , { document : dom . window . document } )
97
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `""` )
98
+ } )
99
+
100
+ it ( 'null fn return' , async ( ) => {
101
+ const dom = useDom ( )
102
+ const head = createClientHeadWithContext ( {
103
+ document : dom . window . document ,
104
+ } )
105
+ head . push ( {
106
+ titleTemplate : ( title ?: string ) => title === 'test' ? null : `${ title } - Template` ,
107
+ title : 'test' ,
108
+ } )
109
+ await renderDOMHead ( head , { document : dom . window . document } )
110
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `""` )
111
+ } )
112
+
113
+ it ( 'empty title' , async ( ) => {
114
+ const dom = useDom ( )
115
+ const head = createClientHeadWithContext ( {
116
+ document : dom . window . document ,
117
+ } )
118
+ head . push ( {
119
+ title : '' ,
120
+ } )
121
+ await renderDOMHead ( head , { document : dom . window . document } )
122
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `""` )
123
+ } )
124
+
125
+ it ( 'replacing title with empty' , async ( ) => {
126
+ const dom = useDom ( )
127
+ const head = createClientHeadWithContext ( {
128
+ document : dom . window . document ,
129
+ } )
130
+ head . push ( {
131
+ title : 'test' ,
132
+ } )
133
+ head . push ( {
134
+ title : '' ,
135
+ } )
136
+ await renderDOMHead ( head , { document : dom . window . document } )
137
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `""` )
138
+ } )
139
+ it ( '#513' , async ( ) => {
140
+ const dom = useDom ( )
141
+ createClientHeadWithContext ( {
142
+ document : dom . window . document ,
143
+ init : [
144
+ { titleTemplate : title => `${ title ?? '' } — Jetfly Group` . replace ( / ^ — / , '' ) } ,
145
+ ] ,
146
+ } )
147
+
148
+ // wait
149
+ await new Promise ( resolve => setTimeout ( resolve , 100 ) )
150
+
151
+ expect ( dom . window . document . title ) . toMatchInlineSnapshot ( `"Jetfly Group"` )
152
+ } )
128
153
} )
0 commit comments