<commit_before>import checkUpdate from '@/_helpers/check-update'
import _fetchMock, { FetchMock } from 'jest-fetch-mock'
import { browser } from '../../helper'

const fetchMock = _fetchMock as FetchMock

describe('Check Update', () => {
  beforeAll(() => {
    window.fetch = fetchMock
    browser.runtime.getManifest.returns({
      version: '1.1.1'
    })
  })
  beforeEach(() => {
    fetchMock.resetMocks()
  })

  it('Server Got Same Version (Not Available)', () => {
    const responseObj = { tag_name: 'v1.1.1' }
    const resolveSpy = jest.fn()
    const rejectSpy = jest.fn()
    const catchSpy = jest.fn()

    fetchMock.mockResponseOnce(JSON.stringify(responseObj))

    const p = checkUpdate()
      .then(resolveSpy, rejectSpy)
      .catch(catchSpy)
      .then(() => {
        expect(resolveSpy).toHaveBeenCalledTimes(1)
        expect(rejectSpy).toHaveBeenCalledTimes(0)
        expect(catchSpy).toHaveBeenCalledTimes(0)
        expect(resolveSpy).toBeCalledWith(
          expect.objectContaining({
            isAvailable: false
          })
        )
      })
    expect(fetchMock).toHaveBeenCalledTimes(1)
    return p
  })
  ;[
    ['Patch', 'v1.1.2', 'v1.1.0'],
    ['Minor', 'v1.2.1', 'v1.0.1'],
    ['Major', 'v2.1.1', 'v0.1.1']
  ].forEach(([title, newerVersion, olderVersion]) => {
    describe(title, () => {
      it('New Version Available', () => {
        const responseObj = { tag_name: newerVersion }
        const resolveSpy = jest.fn()
        const rejectSpy = jest.fn()
        const catchSpy = jest.fn()

        fetchMock.mockResponseOnce(JSON.stringify(responseObj))

        const p = checkUpdate()
          .then(resolveSpy, rejectSpy)
          .catch(catchSpy)
          .then(() => {
            expect(resolveSpy).toHaveBeenCalledTimes(1)
            expect(rejectSpy).toHaveBeenCalledTimes(0)
            expect(catchSpy).toHaveBeenCalledTimes(0)
            expect(resolveSpy).toBeCalledWith({
              isAvailable: true,
              info: responseObj
            })
          })
        expect(fetchMock).toHaveBeenCalledTimes(1)
        return p
      })

      it('Server Got Older Version (Not Available)', () => {
        const responseObj = { tag_name: olderVersion }
        const resolveSpy = jest.fn()
        const rejectSpy = jest.fn()
        const catchSpy = jest.fn()

        fetchMock.mockResponseOnce(JSON.stringify(responseObj))

        const p = checkUpdate()
          .then(resolveSpy, rejectSpy)
          .catch(catchSpy)
          .then(() => {
            expect(resolveSpy).toHaveBeenCalledTimes(1)
            expect(rejectSpy).toHaveBeenCalledTimes(0)
            expect(catchSpy).toHaveBeenCalledTimes(0)
            expect(resolveSpy).toBeCalledWith(
              expect.objectContaining({
                isAvailable: false
              })
            )
          })
        expect(fetchMock).toHaveBeenCalledTimes(1)
        return p
      })<commit_msg>test: update check-update<commit_after>import { checkUpdate } from '@/_helpers/check-update'
import _fetchMock, { FetchMock } from 'jest-fetch-mock'
import getDefaultConfig from '@/app-config'

const fetchMock = _fetchMock as FetchMock

describe('Check Update', () => {
  beforeAll(() => {
    window.fetch = fetchMock
  })

  beforeEach(() => {
    fetchMock.resetMocks()
    window.appConfig = getDefaultConfig()
  })

  const tests = [
    ['Same', 'v1.1.1', 'v1.1.1', 0],
    ['Newer Patch', 'v1.1.2', 'v1.1.0', 1],
    ['Newer Minor', 'v1.2.1', 'v1.0.1', 2],
    ['Newer Major', 'v2.1.1', 'v0.1.1', 3],
    ['Older Patch', 'v1.1.0', 'v1.1.2', -1],
    ['Older Minor', 'v1.0.1', 'v1.2.1', -2],
    ['Older Major', 'v0.1.1', 'v2.1.1', -3]
  ] as const

  tests.forEach(([title, newerVersion, olderVersion, diff]) => {
    it(title, async () => {
      const responseObj = { version: newerVersion }
      const resolveSpy = jest.fn()
      const rejectSpy = jest.fn()
      const catchSpy = jest.fn()

      fetchMock.mockResponseOnce(JSON.stringify(responseObj))

      await checkUpdate(olderVersion.slice(1))
        .then(resolveSpy, rejectSpy)
        .catch(catchSpy)

      expect(fetchMock).toHaveBeenCalledTimes(1)
      expect(resolveSpy).toHaveBeenCalledTimes(1)
      expect(rejectSpy).toHaveBeenCalledTimes(0)
      expect(catchSpy).toHaveBeenCalledTimes(0)
      expect(resolveSpy).toBeCalledWith({
        diff,
        data: responseObj
      })
{'fraction_non_alphanumeric': 0.14726571363739505, 'fraction_numerical': 0.02042205582028591, 'mean_word_length': 2.484584980237154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6258490', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1439, 'n_words': 250}