Skip to content

Commit 76790a3

Browse files
committed
for eta-dev#99 eta-dev#102: add tests for globalAwait option
1 parent 353ceb7 commit 76790a3

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

test/async.spec.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,30 @@
33
import * as Eta from '../src/index'
44
import { buildRegEx } from './err.spec'
55

6-
function resolveAfter2Seconds(val: string): Promise<string> {
6+
type AsyncTimes = {time: number, type: "start" | "end"}[]
7+
8+
function resolveAfter20Milliseconds(val: string): Promise<string> {
79
return new Promise((resolve) => {
810
setTimeout(() => {
911
resolve(val)
1012
}, 20)
1113
})
1214
}
1315

16+
function createWaitAndStoreTimes(times: AsyncTimes): (val: string) => Promise<string> {
17+
return val => new Promise<string>((resolve) => {
18+
times.push({time: Date.now(), type: 'start'});
19+
setTimeout(() => {
20+
resolve(val)
21+
}, 200)
22+
}).then(x => {
23+
times.push({time: Date.now(), type: 'end'});
24+
return x;
25+
})
26+
}
27+
1428
async function asyncTest() {
15-
const result = await resolveAfter2Seconds('HI FROM ASYNC')
29+
const result = await resolveAfter20Milliseconds('HI FROM ASYNC')
1630
return result
1731
}
1832

@@ -56,6 +70,59 @@ if(cb){cb(null,tR)} return tR
5670
)
5771
})
5872

73+
it('does not await code in eval tags with globalAwait option', async () => {
74+
expect(
75+
await Eta.render('Hi <% if (it.name) { %>0<% } %>', { name: Promise.resolve(false) }, { async: true, globalAwait: true })
76+
).toEqual('Hi 0')
77+
})
78+
79+
it('automatically awaits promises with the globalAwait option', async () => {
80+
expect(
81+
await Eta.render('Hi <%= it.name %>', { name: Promise.resolve('Ada Lovelace') }, { async: true, globalAwait: true })
82+
).toEqual('Hi Ada Lovelace')
83+
})
84+
85+
it('globalAwait option works with an empty interpolation tag', async () => {
86+
expect(
87+
await Eta.render('Hi <%=%>', { }, { async: true, globalAwait: true })
88+
).toEqual('Hi undefined')
89+
})
90+
91+
it('awaits all promises in parallel with the globalAwait option (escaped)', async () => {
92+
const times: AsyncTimes = [];
93+
const wait = createWaitAndStoreTimes(times)
94+
const result = await Eta.render('Hi <%= it.wait(1) %> <%= it.wait(2) %> <%= it.wait(3) %>', { wait }, { async: true, globalAwait: true })
95+
expect(result).toEqual('Hi 1 2 3')
96+
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
97+
expect(times.map(t => t.type).join(',')).toBe('start,start,start,end,end,end')
98+
})
99+
100+
it('awaits all promises in sequence without the globalAwait option (escaped)', async () => {
101+
const times: AsyncTimes = [];
102+
const wait = createWaitAndStoreTimes(times)
103+
const result = await Eta.render('Hi <%= await it.wait(1) %> <%= await it.wait(2) %> <%= await it.wait(3) %>', { wait }, { async: true, globalAwait: false })
104+
expect(result).toEqual('Hi 1 2 3')
105+
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
106+
expect(times.map(t => t.type).join(',')).toBe('start,end,start,end,start,end')
107+
})
108+
109+
it('awaits all promises in parallel with the globalAwait option (raw)', async () => {
110+
const times: AsyncTimes = [];
111+
const wait = createWaitAndStoreTimes(times)
112+
const result = await Eta.render('Hi <%~ it.wait(1) %> <%~ it.wait(2) %> <%~ it.wait(3) %>', { wait }, { async: true, globalAwait: true })
113+
expect(result).toEqual('Hi 1 2 3')
114+
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
115+
expect(times.map(t => t.type).join(',')).toBe('start,start,start,end,end,end')
116+
})
117+
118+
it('awaits all promises in sequence without the globalAwait option (raw)', async () => {
119+
const times: AsyncTimes = [];
120+
const wait = createWaitAndStoreTimes(times)
121+
const result = await Eta.render('Hi <%~ await it.wait(1) %> <%~ await it.wait(2) %> <%~ await it.wait(3) %>', { wait }, { async: true, globalAwait: false })
122+
expect(result).toEqual('Hi 1 2 3')
123+
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
124+
expect(times.map(t => t.type).join(',')).toBe('start,end,start,end,start,end')
125+
})
59126
it('Async template w/ syntax error passes error to callback', (done) => {
60127
function cb(err: Error | null, _res?: string) {
61128
expect(err).toBeTruthy()

test/render.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ describe('Simple Render checks', () => {
1414
'Hi Ada Lovelace!'
1515
)
1616
})
17+
it('Empty interpolation tag works', () => {
18+
expect(render(compile('Hi <%=%>'), {})).toEqual('Hi undefined')
19+
})
1720
it('Rendering function works', () => {
1821
expect(render(compile('Hi \n<%- =it.name_%> !'), { name: 'Ada Lovelace' })).toEqual(
1922
'Hi Ada Lovelace!'

0 commit comments

Comments
 (0)