Skip to content

Commit

Permalink
more tet fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jerader committed Mar 5, 2024
1 parent 3bf03d6 commit f654e35
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
1 change: 0 additions & 1 deletion __mocks__/electron-store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// mock electron-store
'use strict'
import { vi } from 'vitest'
import { DEFAULTS_V12 } from '../app-shell-odd/src/config/migrate'

const Store = function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// TODO(mc, 2020-06-11): test all release-files functions
import { vi, describe, it, expect, afterAll } from 'vitest'
import path from 'path'
import { describe, afterAll, it } from 'vitest'
import { promises as fs } from 'fs'
import fse from 'fs-extra'
import tempy from 'tempy'
Expand Down
36 changes: 15 additions & 21 deletions app-shell-odd/src/system-update/__tests__/release-manifest.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
import { describe, it, vi, beforeEach, afterEach } from 'vitest'
import fse from 'fs-extra'
import { describe, it, vi, beforeEach, afterEach, expect } from 'vitest'
import * as Http from '../../http'
import * as Dirs from '../directories'
import { downloadAndCacheReleaseManifest } from '../release-manifest'

vi.mock('fs-extra')
vi.mock('../../http')
vi.mock('../directories')

vi.mock('electron-store')
const fetchJson = Http.fetchJson
const outputJson = fse.outputJson
const readJson = fse.readJson
const getManifestCacheDir = Dirs.getManifestCacheDir

const MOCK_DIR = 'mock_dir'
const MANIFEST_URL = 'http://example.com/releases.json'
const MOCK_MANIFEST = {}
const MOCK_MANIFEST = {} as any

describe('release manifest utilities', () => {
beforeEach(() => {
vi.mocked(getManifestCacheDir).mockReturnValue(MOCK_DIR)
vi.mocked(fetchJson).mockResolvedValue(MOCK_MANIFEST)
vi.mocked(outputJson).mockResolvedValue()
vi.mocked(readJson)
// @ts-expect-error readJson takes additional optional arguments which is tweaking jest-when
.mockResolvedValue(MOCK_MANIFEST)
})

afterEach(() => {
vi.resetAllMocks()
})

it('should download and save the manifest from a url', () => {
return downloadAndCacheReleaseManifest(MANIFEST_URL).then(manifest => {
expect(manifest).toBe(MOCK_MANIFEST)
expect(outputJson).toHaveBeenCalledWith(MOCK_DIR, MOCK_MANIFEST)
})
it('should download and save the manifest from a url', async () => {
await expect(
downloadAndCacheReleaseManifest(MANIFEST_URL)
).resolves.toEqual(MOCK_MANIFEST)
expect(fetchJson).toHaveBeenCalledWith(MANIFEST_URL)
})

it('should pull the manifest from the file if the manifest download fails', () => {
vi.mocked(fetchJson).mockRejectedValue('oh no!')
return downloadAndCacheReleaseManifest(MANIFEST_URL).then(manifest =>
expect(manifest).toBe(MOCK_MANIFEST)
)
it('should pull the manifest from the file if the manifest download fails', async () => {
const error = new Error('Failed to download')
vi.mocked(fetchJson).mockRejectedValue(error)
await expect(
downloadAndCacheReleaseManifest(MANIFEST_URL)
).resolves.toEqual(MOCK_MANIFEST)
expect(fetchJson).toHaveBeenCalledWith(MANIFEST_URL)
})
})
9 changes: 2 additions & 7 deletions app-shell/src/protocol-storage/__tests__/file-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path'
import fs from 'fs-extra'
import tempy from 'tempy'
import Electron from 'electron'
import { vi, describe, beforeEach, it, afterAll } from 'vitest'
import { vi, describe, beforeEach, it, afterAll, expect } from 'vitest'
import uuid from 'uuid/v4'

import {
Expand All @@ -16,14 +16,12 @@ import {
PROTOCOLS_DIRECTORY_NAME,
PROTOCOLS_DIRECTORY_PATH,
} from '../file-system'
import { getConfig } from '../../config'
import { analyzeProtocolSource } from '../../protocol-analysis'


vi.mock('uuid/v4')
vi.mock('electron')
vi.mock('../../config')
vi.mock('../../protocol-analysis')
vi.mock('electron-store')

const trashItem = Electron.shell.trashItem

Expand All @@ -37,9 +35,6 @@ describe('protocol storage directory utilities', () => {
}
beforeEach(() => {
protocolsDir = makeEmptyDir()
vi.mocked(getConfig).mockReturnValue({
python: { pathToPythonOverride: null },
} as any)
vi.mocked(analyzeProtocolSource).mockReturnValue(Promise.resolve())
})

Expand Down

0 comments on commit f654e35

Please sign in to comment.