Skip to content

Commit

Permalink
for eta-dev#99 eta-dev#102: add tests for globalAwait option
Browse files Browse the repository at this point in the history
  • Loading branch information
blutorange committed Nov 28, 2021
1 parent 353ceb7 commit 76790a3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
71 changes: 69 additions & 2 deletions test/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
import * as Eta from '../src/index'
import { buildRegEx } from './err.spec'

function resolveAfter2Seconds(val: string): Promise<string> {
type AsyncTimes = {time: number, type: "start" | "end"}[]

function resolveAfter20Milliseconds(val: string): Promise<string> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(val)
}, 20)
})
}

function createWaitAndStoreTimes(times: AsyncTimes): (val: string) => Promise<string> {
return val => new Promise<string>((resolve) => {
times.push({time: Date.now(), type: 'start'});
setTimeout(() => {
resolve(val)
}, 200)
}).then(x => {
times.push({time: Date.now(), type: 'end'});
return x;
})
}

async function asyncTest() {
const result = await resolveAfter2Seconds('HI FROM ASYNC')
const result = await resolveAfter20Milliseconds('HI FROM ASYNC')
return result
}

Expand Down Expand Up @@ -56,6 +70,59 @@ if(cb){cb(null,tR)} return tR
)
})

it('does not await code in eval tags with globalAwait option', async () => {
expect(
await Eta.render('Hi <% if (it.name) { %>0<% } %>', { name: Promise.resolve(false) }, { async: true, globalAwait: true })
).toEqual('Hi 0')
})

it('automatically awaits promises with the globalAwait option', async () => {
expect(
await Eta.render('Hi <%= it.name %>', { name: Promise.resolve('Ada Lovelace') }, { async: true, globalAwait: true })
).toEqual('Hi Ada Lovelace')
})

it('globalAwait option works with an empty interpolation tag', async () => {
expect(
await Eta.render('Hi <%=%>', { }, { async: true, globalAwait: true })
).toEqual('Hi undefined')
})

it('awaits all promises in parallel with the globalAwait option (escaped)', async () => {
const times: AsyncTimes = [];
const wait = createWaitAndStoreTimes(times)
const result = await Eta.render('Hi <%= it.wait(1) %> <%= it.wait(2) %> <%= it.wait(3) %>', { wait }, { async: true, globalAwait: true })
expect(result).toEqual('Hi 1 2 3')
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
expect(times.map(t => t.type).join(',')).toBe('start,start,start,end,end,end')
})

it('awaits all promises in sequence without the globalAwait option (escaped)', async () => {
const times: AsyncTimes = [];
const wait = createWaitAndStoreTimes(times)
const result = await Eta.render('Hi <%= await it.wait(1) %> <%= await it.wait(2) %> <%= await it.wait(3) %>', { wait }, { async: true, globalAwait: false })
expect(result).toEqual('Hi 1 2 3')
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
expect(times.map(t => t.type).join(',')).toBe('start,end,start,end,start,end')
})

it('awaits all promises in parallel with the globalAwait option (raw)', async () => {
const times: AsyncTimes = [];
const wait = createWaitAndStoreTimes(times)
const result = await Eta.render('Hi <%~ it.wait(1) %> <%~ it.wait(2) %> <%~ it.wait(3) %>', { wait }, { async: true, globalAwait: true })
expect(result).toEqual('Hi 1 2 3')
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
expect(times.map(t => t.type).join(',')).toBe('start,start,start,end,end,end')
})

it('awaits all promises in sequence without the globalAwait option (raw)', async () => {
const times: AsyncTimes = [];
const wait = createWaitAndStoreTimes(times)
const result = await Eta.render('Hi <%~ await it.wait(1) %> <%~ await it.wait(2) %> <%~ await it.wait(3) %>', { wait }, { async: true, globalAwait: false })
expect(result).toEqual('Hi 1 2 3')
times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0)
expect(times.map(t => t.type).join(',')).toBe('start,end,start,end,start,end')
})
it('Async template w/ syntax error passes error to callback', (done) => {
function cb(err: Error | null, _res?: string) {
expect(err).toBeTruthy()
Expand Down
3 changes: 3 additions & 0 deletions test/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe('Simple Render checks', () => {
'Hi Ada Lovelace!'
)
})
it('Empty interpolation tag works', () => {
expect(render(compile('Hi <%=%>'), {})).toEqual('Hi undefined')
})
it('Rendering function works', () => {
expect(render(compile('Hi \n<%- =it.name_%> !'), { name: 'Ada Lovelace' })).toEqual(
'Hi Ada Lovelace!'
Expand Down

0 comments on commit 76790a3

Please sign in to comment.