const mockClean = jest.fn()
const cb = jest.fn()
jest.mock('../../gulpfile.ts/internal')
jest.mock('del', () => ({
  __esModule: true,
  default: mockClean,
}))
jest.mock('chalk')
jest.mock('fancy-log')

import * as internal from '../../gulpfile.ts/internal'
import { TIMPLA_DEFAULTS } from '../../gulpfile.ts/lib/TIMPLA_DEFAULTS'
import { IFullTimplaConfig } from '../../gulpfile.ts/lib/TIMPLA_INTERFACES'
import { clean as cleanTask } from '../../gulpfile.ts/tasks/clean'
import { clone } from '../helpers'

beforeEach(() => {
  jest.resetAllMocks()
})

describe('cleanTask', () => {
  it('should default to the dest path when no patterns are supplied', () => {
    const conf: IFullTimplaConfig = clone(TIMPLA_DEFAULTS)
    if (!conf.clean) {
      return
    }

    const clean = cleanTask(conf)
    const spyProjectSrcPath = jest.spyOn(internal, 'projectSrcPath')
    const spyProjectDestPath = jest.spyOn(internal, 'projectDestPath')
    spyProjectDestPath.mockReturnValue('dest')
    spyProjectSrcPath.mockReturnValue('src')
    mockClean.mockReturnValue('hi')

    const result = clean(cb)

    expect(spyProjectSrcPath).toHaveBeenCalled()
    expect(spyProjectDestPath).toHaveBeenCalled()
    expect(mockClean).toHaveBeenCalledWith(['dest'], {
      force: true,
    })
    expect(result).toEqual('hi')
  })
  it('should delete the supplied patterns', () => {
    const conf: IFullTimplaConfig = clone(TIMPLA_DEFAULTS)
    conf.clean = {
      delOptions: {
        dryRun: true,
      },
      patterns: ['hello'],
    }
    if (!conf.clean) {
      return
    }

    const spyProjectSrcPath = jest.spyOn(internal, 'projectSrcPath')
    const spyProjectDestPath = jest.spyOn(internal, 'projectDestPath')
    spyProjectDestPath.mockReturnValue('dest')
    spyProjectSrcPath.mockReturnValue('src')

    const clean = cleanTask(conf)
    clean(cb)
    expect(mockClean).toHaveBeenCalledWith(['hello'], {
      dryRun: true,
    })
  })
  it('should not delete invalid paths', () => {
    const conf: IFullTimplaConfig = clone(TIMPLA_DEFAULTS)
    conf.clean = {
      patterns: ['invalid'],
    }
    const spyProjectSrcPath = jest.spyOn(internal, 'projectSrcPath')
    spyProjectSrcPath.mockReturnValue('invalid')

    const clean = cleanTask(conf)
    try {
      clean(cb)
    } catch (e) {
      expect(e).toBeTruthy()
    }
  })
  it('should call the gulp cb if the task is disabled', () => {
    const conf: IFullTimplaConfig = clone(TIMPLA_DEFAULTS)
    conf.clean = false
    const clean = cleanTask(conf)
    clean(cb)
    expect(cb).toHaveBeenCalled()
  })
})
