<commit_before>jest.mock('../utils/glob')
jest.mock('fs-promise')
jest.mock('chokidar')
jest.mock('../utils/glob')
jest.mock('../utils/server')
jest.mock('../utils/log')
jest.mock('../utils/getConfig')
jest.mock('../utils/renderer')
jest.mock('../plugins/addTOC')
jest.mock('../plugins/load')

const chokidar = require('chokidar')
const Wikic = require('../index')
const glob = require('../utils/glob')
const logger = require('../utils/log')
const fsp = require('fs-promise')

/* eslint-disable no-underscore-dangle */
glob.__setPath('**/*', ['404.md', 'styles/main.css'])
glob.__setPath('**/*.md', ['css/flexbox.md', 'html/hi.md'])
glob.__setPath('**/', ['css/', 'html/'])
/* eslint-enable no-underscore-dangle */

const callThat = (key, afterCalling) => {
  it(`calls ${key}`, async () => {
    const wikic = new Wikic()
    const spy = jest.spyOn(wikic, key)
    await wikic[afterCalling]()
    expect(spy).toHaveBeenCalled()
    spy.mockReset()
    spy.mockRestore()
  })
}

describe('wikic build', () => {
  describe('setup', () => {
    callThat('setConfig', 'setup')
  })

  describe('setConfig', () => {
    callThat('configureRenderer', 'setConfig')
  })

  describe('afterRead & beforeWrite', () => {
    it('beforeWrite works', async () => {
      const wikic = new Wikic()
      const callback = jest.fn(context => context)
      const length = wikic.beforeWriteTasks.length

      expect(wikic.beforeWrite(callback)).toBe(wikic)
      expect(wikic.beforeWriteTasks.length).toBe(length + 1)
      await wikic.build()
      expect(callback.mock.calls.length).not.toBeLessThan(1)
    })

    it('beforeWrite throws if not pass a function', () => {
      const wikic = new Wikic()
      try {
        wikic.beforeWrite()
      } catch (e) {
        expect(e.message).toBe('should pass a function')
      }
    })

    it('afterRead works', async () => {
      const wikic = new Wikic()
      const callback = jest.fn(context => context)
      const length = wikic.afterReadTasks.length

      expect(wikic.afterRead(callback)).toBe(wikic)
      expect(wikic.afterReadTasks.length).toBe(length + 1)
      await wikic.build()
      expect(callback.mock.calls.length).not.toBeLessThan(1)
    })

    it('afterRead throws if not pass a function', () => {
      const wikic = new Wikic()
      try {
        wikic.afterRead()
      } catch (e) {
        expect(e.message).toBe('should pass a function')
      }
    })
  })

  describe('build', () => {
    it('call build() will call once only', () => {
      const wikic = new Wikic()
      wikic.build()
      wikic.build()
      expect(logger.error.mock.calls.length).toBe(1)
      logger.error.mockClear()
    })
  })

  describe('readMD', () => {
    it('works if with from in context', async () => {
      const wikic = new Wikic()
      await wikic.readMD({
        data: '',
        config: {},
      })
    })

    it('works if set skipRead and afterReadTasks', async () => {
      const wikic = new Wikic()
      await wikic.readMD({
        from: '/path/to/',
        data: '',
        config: {},
        afterReadTasks: [],
        skipRead: true,
      })
    })
  })
})

describe('watch', () => {
  /* eslint-disable  no-underscore-dangle */
  it('works', () => {
    const wikic = new Wikic()
    wikic.watch()
    const setup = jest.spyOn(wikic, 'setup')
    const build = jest.spyOn(wikic, 'build')

    chokidar.__emiter.emit('change', '/path/to/')
    expect(setup).toHaveBeenCalled()
    expect(build).toHaveBeenCalled()
    setup.mockReset()
    setup.mockRestore()
    build.mockReset()
    build.mockRestore()

    chokidar.__emiter.emit('unlink', 'text.txt')
    expect(fsp.removeSync.mock.calls[0][0]).toMatch('docs/text.txt')

    chokidar.__emiter.emit('error', Error('oops'))
    expect(logger.error.mock.calls.length).toBe(1)
    expect(logger.error.mock.calls[0][0]).toMatch('Error: oops')
  })
})<commit_msg>test(index): add more test for clean() and watch's ignored pattern<commit_after>/* eslint-disable no-underscore-dangle */

jest.mock('../utils/glob')
jest.mock('fs-promise')
jest.mock('chokidar')
jest.mock('../utils/glob')
jest.mock('../utils/server')
jest.mock('../utils/log')
jest.mock('../utils/getConfig')
jest.mock('../utils/renderer')
jest.mock('../plugins/addTOC')
jest.mock('../plugins/load')

const chokidar = require('chokidar')
const Wikic = require('../index')
const glob = require('../utils/glob')
const logger = require('../utils/log')
const fsp = require('fs-promise')

glob.__setPath('**/*', ['404.md', 'styles/main.css'])
glob.__setPath('**/*.md', ['css/flexbox.md', 'html/hi.md'])
glob.__setPath('**/', ['css/', 'html/'])

const callThat = (key, afterCalling) => {
  it(`calls ${key}`, async () => {
    const wikic = new Wikic()
    const spy = jest.spyOn(wikic, key)
    await wikic[afterCalling]()
    expect(spy).toHaveBeenCalled()
    spy.mockReset()
    spy.mockRestore()
  })
}

describe('wikic build', () => {
  describe('setup', () => {
    callThat('setConfig', 'setup')
  })

  describe('setConfig', () => {
    callThat('configureRenderer', 'setConfig')
  })

  describe('afterRead & beforeWrite', () => {
    it('beforeWrite works', async () => {
      const wikic = new Wikic()
      const callback = jest.fn(context => context)
      const length = wikic.beforeWriteTasks.length

      expect(wikic.beforeWrite(callback)).toBe(wikic)
      expect(wikic.beforeWriteTasks.length).toBe(length + 1)
      await wikic.build()
      expect(callback.mock.calls.length).not.toBeLessThan(1)
    })

    it('beforeWrite throws if not pass a function', () => {
      const wikic = new Wikic()
      try {
        wikic.beforeWrite()
      } catch (e) {
        expect(e.message).toBe('should pass a function')
      }
    })

    it('afterRead works', async () => {
      const wikic = new Wikic()
      const callback = jest.fn(context => context)
      const length = wikic.afterReadTasks.length

      expect(wikic.afterRead(callback)).toBe(wikic)
      expect(wikic.afterReadTasks.length).toBe(length + 1)
      await wikic.build()
      expect(callback.mock.calls.length).not.toBeLessThan(1)
    })

    it('afterRead throws if not pass a function', () => {
      const wikic = new Wikic()
      try {
        wikic.afterRead()
      } catch (e) {
        expect(e.message).toBe('should pass a function')
      }
    })
  })

  describe('build', () => {
    it('call build() will call once only', () => {
      const wikic = new Wikic()
      wikic.build()
      wikic.build()
      expect(logger.error.mock.calls.length).toBe(1)
      logger.error.mockClear()
    })
  })

  describe('readMD', () => {
    it('works if with from in context', async () => {
      const wikic = new Wikic()
      await wikic.readMD({
        data: '',
        config: {},
      })
    })

    it('works if set skipRead and afterReadTasks', async () => {
      const wikic = new Wikic()
      await wikic.readMD({
        from: '/path/to/',
        data: '',
        config: {},
        afterReadTasks: [],
        skipRead: true,
      })
    })
  })
})

describe('watch', () => {
  it('works', () => {
    const wikic = new Wikic()
    wikic.watch()
    const setup = jest.spyOn(wikic, 'setup')
    const build = jest.spyOn(wikic, 'build')

    chokidar.__emiter.emit('change', '/path/to/')
    expect(setup).toHaveBeenCalled()
    expect(build).toHaveBeenCalled()
    setup.mockReset()
    setup.mockRestore()
    build.mockReset()
    build.mockRestore()

    chokidar.__emiter.emit('unlink', 'text.txt')
    expect(fsp.removeSync.mock.calls[0][0]).toMatch('docs/text.txt')

    chokidar.__emiter.emit('error', Error('oops'))
    expect(logger.error.mock.calls.length).toBe(1)
    expect(logger.error.mock.calls[0][0]).toMatch('Error: oops')
  })

  test('ignored', () => {
    const wikic = new Wikic()

    chokidar.watch.mockClear()
    wikic.config.layoutPath = '_layout'
    wikic.config.docsPath = '_doc'
    wikic.watch()
    expect(chokidar.watch.mock.calls[0][1].ignored.pop()).toBe('_!(layout|doc)/**')

    chokidar.watch.mockClear()
    wikic.config.layoutPath = 'layout'
    wikic.config.docsPath = 'doc'
    wikic.watch()
    expect(chokidar.watch.mock.calls[0][1].ignored.pop()).toBe('_*/**')
  })
})

describe('clean', async () => {
  const wikic = new Wikic()
  expect(await wikic.clean()).toBe(wikic)
  expect(fsp.emptyDir.mock.calls[0][0]).toBe(wikic.publicPath)
})
{'fraction_non_alphanumeric': 0.1892955060197878, 'fraction_numerical': 0.003814519012993205, 'mean_word_length': 2.937118723603942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1853219', 'n_tokens_mistral': 2924, 'n_tokens_neox': 2881, 'n_words': 494}