Skip to content

Commit

Permalink
Add test and make them pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovannini committed Jul 11, 2022
1 parent f0cf593 commit 7154eab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/vite-plugin-watch-and-run/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export type Options = {
name?: string | null
}

const kindWithPath = ['add', 'addDir', 'change', 'delete', 'unlink', 'unlinkDir'] as const
export const kindWithPath = ['add', 'addDir', 'change', 'delete', 'unlink', 'unlinkDir'] as const
export type KindWithPath = typeof kindWithPath[number]
const kindWithoutPath = ['all', 'error', 'raw', 'ready'] as const
export const kindWithoutPath = ['all', 'error', 'raw', 'ready'] as const
export type KindWithoutPath = typeof kindWithoutPath[number]
export type WatchKind = KindWithPath | KindWithoutPath

Expand Down
24 changes: 17 additions & 7 deletions packages/vite-plugin-watch-and-run/test/plugins.checkConf.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import watchAndRun from '../src'
import watchAndRun, { KindWithoutPath, kindWithoutPath, kindWithPath } from '../src'
import { describe, expect, it, vi } from 'vitest'

describe('vite-plugin-watch-and-run', () => {
it('Should throw an error as no config is sent', async () => {
const t = () => {
watchAndRun(null)
watchAndRun(null as any)
}
expect(t).toThrowErrorMatchingInlineSnapshot('"plugin watchAndRun, `params` needs to be an array."')
})
Expand Down Expand Up @@ -37,14 +37,24 @@ describe('vite-plugin-watch-and-run', () => {
expect(plugin.watchAndRunConf).to.have.property(watch).to.have.property('delay', 0)
})

it('Should have a valid conf, with default watchKind:ADD / CHANGE / DELETE', async () => {
it('Should have a valid conf, with default watchKind:add / change / delete', async () => {
const watch = '**/*.(gql|graphql)'
const plugin = watchAndRun([{ watch, run: 'yarn gen' }])

expect(plugin.watchAndRunConf)
.to.have.property(watch)
.to.have.property('kind')
.to.have.all.members(['ADD', 'CHANGE', 'DELETE'])
.to.have.all.members(['add', 'change', 'delete'])
})

it('Should run on ready state too', async () => {
const watch = '**/*.(gql|graphql)'
const plugin = watchAndRun([{ watch, run: 'yarn gen', watchKind: ['add', 'change', 'delete', 'ready'] }])

expect(plugin.watchAndRunConf)
.to.have.property(watch)
.to.have.property('kind')
.to.have.all.members(['add', 'change', 'delete', 'ready'])
})

it('Should register all watchers', async () => {
Expand All @@ -57,15 +67,15 @@ describe('vite-plugin-watch-and-run', () => {
},
}
const spy = vi.spyOn(server.watcher, 'on').mockImplementation((type: 'add' | 'change' | 'delete', callback) => {
if (type === 'add' || type === 'change' || type === 'delete') {
if (kindWithPath.includes(type) || kindWithoutPath.includes(type as KindWithoutPath)) {
// eslint-disable-next-line unicorn/no-lonely-if
if (typeof callback === 'function') return 'registered'
}
return 'error'
})
plugin.configureServer(server)
expect(spy).toHaveBeenCalledTimes(3)
const operations = ['add', 'change', 'delete']
expect(spy).toHaveBeenCalledTimes(10)
const operations = ['add', 'addDir', 'change', 'delete', 'unlink', 'unlinkDir', /**/ 'all', 'error', 'raw', 'ready']
spy.mock.calls.forEach((call, index) => {
expect(spy).toHaveBeenNthCalledWith(index + 1, operations[index], call[1])
expect(spy).toHaveNthReturnedWith(index + 1, 'registered')
Expand Down

0 comments on commit 7154eab

Please sign in to comment.