Skip to content

Commit

Permalink
Fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Jan 14, 2017
1 parent 1341ca2 commit b4eed5f
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions test/specs/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,37 @@ describe('watch', () => {
watcher.on('add', once(() => {
test(p('test.vue.d.ts'), 'export declare const test: string;')
done()
}, true))
}))

fs.writeFileSync(p('test.vue'), vue('export const test: string = ""'))
fs.writeFile(p('test.vue'), vue('export const test: string = ""'))
})

it('updates d.ts if .vue file is updated', done => {
watcher.on('add', once(() => {
test(p('test.vue.d.ts'), 'export declare const test: string;')
fs.writeFile(p('test.vue'), vue('export const foo: number = 1'))
}))

watcher.on('change', once(() => {
test(p('test.vue.d.ts'), 'export declare const foo: number;')
done()
}))
}, 1))

fs.writeFileSync(p('test.vue'), vue('export const test: string = ""'))
fs.writeFileSync(p('test.vue'), vue('export const foo: number = 1'))
fs.writeFile(p('test.vue'), vue('export const test: string = ""'))
})

it('removes d.ts if corresponding .vue file is removed', done => {
watcher.on('add', once(() => {
assert.ok(fs.existsSync(p('test.vue.d.ts')))
fs.unlinkSync(p('test.vue'))
fs.unlink(p('test.vue'))
}))

watcher.on('unlink', once(() => {
assert.ifError(fs.existsSync(p('test.vue.d.ts')))
done()
}))

fs.writeFileSync(p('test.vue'), vue('export const test: string = ""'))
fs.writeFile(p('test.vue'), vue('export const test: string = ""'))
})

it('allows re-add .vue file', done => {
Expand All @@ -61,19 +65,18 @@ describe('watch', () => {
watcher.on('add', once(() => {
test(p('test.vue.d.ts'), 'export declare let b: boolean;')
done()
}, true))
}))

fs.writeFileSync(p('test.vue'), vue('export declare let b: boolean'))
fs.writeFile(p('test.vue'), vue('export declare let b: boolean'))
})
})

// fs.writeFile emits `add` event with empty file data
// so we need to skip first add event in testing
function once (fn: () => void, ignoreFirst: boolean = false): () => void {
function once (fn: () => void, skip: number = 0): (p: string) => void {
let done = false
return () => {
if (ignoreFirst) {
ignoreFirst = false
return path => {
if (!/\.vue.d.ts$/.test(path)) return
if (skip > 0) {
skip -= 1
return
}
if (done) return
Expand Down

0 comments on commit b4eed5f

Please sign in to comment.