Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Dec 10, 2024
1 parent 4172334 commit cac80b8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
89 changes: 86 additions & 3 deletions catalog/app/components/FileEditor/loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import { isSupportedFileType } from './loader'
import { renderHook } from '@testing-library/react-hooks'

import { detect, isSupportedFileType, loadMode, useWriteData } from './loader'

const putObject = jest.fn(async () => ({ VersionId: 'bar' }))

const headObject = jest.fn(async () => ({ VersionId: 'foo', ContentLength: 999 }))

jest.mock(
'constants/config',
'utils/AWS',
jest.fn(() => ({
apiGatewayEndpoint: '',
S3: {
use: jest.fn(() => ({
putObject: () => ({
promise: putObject,
}),
headObject: () => ({
promise: headObject,
}),
})),
},
})),
)

jest.mock(
'constants/config',
jest.fn(() => ({})),
)

jest.mock(
'brace/mode/json',
jest.fn(() => Promise.resolve(undefined)),
)

describe('components/FileEditor/loader', () => {
describe('isSupportedFileType', () => {
it('should return true for supported files', () => {
Expand Down Expand Up @@ -36,4 +61,62 @@ describe('components/FileEditor/loader', () => {
expect(isSupportedFileType('s3://bucket/path/file.bam')).toBe(false)
})
})

describe('detect', () => {
it('should detect quilt_summarize.json', () => {
expect(detect('quilt_summarize.json').map((x) => x.brace)).toEqual([
'__quiltSummarize',
'json',
])
expect(detect('nes/ted/quilt_summarize.json').map((x) => x.brace)).toEqual([
'__quiltSummarize',
'json',
])
})
it('should detect bucket preferences config', () => {
expect(detect('.quilt/catalog/config.yml').map((x) => x.brace)).toEqual([
'__quiltConfig',
'yaml',
])
expect(detect('.quilt/catalog/config.yaml').map((x) => x.brace)).toEqual([
'__quiltConfig',
'yaml',
])
expect(
detect('not/in/root/.quilt/catalog/config.yaml').map((x) => x.brace),
).toEqual(['yaml'])
})
})

describe('useWriteData', () => {
it('rejects when revision is outdated', () => {
const { result } = renderHook(() =>
useWriteData({ bucket: 'a', key: 'b', version: 'c' }),
)
return expect(result.current('any')).rejects.toThrow('Revision is outdated')
})
it('returns new version', () => {
const { result } = renderHook(() =>
useWriteData({ bucket: 'a', key: 'b', version: 'foo' }),
)
return expect(result.current('any')).resolves.toEqual({
bucket: 'a',
key: 'b',
size: 999,
version: 'bar',
})
})
})

describe('loadMode', () => {
it('throws on the first call and resolves on the second', () => {
expect(() => loadMode('json')).toThrow()
return new Promise((resolve) => {
setTimeout(() => {
expect(loadMode('json')).toBe('fulfilled')
resolve(null)
})
})
})
})
})
6 changes: 3 additions & 3 deletions catalog/app/components/FileEditor/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import * as AWS from 'utils/AWS'

import { Mode, EditorInputType } from './types'

const cache: { [index in Mode]?: Promise<void> | 'fullfilled' } = {}
const cache: { [index in Mode]?: Promise<void> | 'fulfilled' } = {}
export const loadMode = (mode: Mode) => {
if (cache[mode] === 'fullfilled') return cache[mode]
if (cache[mode] === 'fulfilled') return cache[mode]
if (cache[mode]) throw cache[mode]

cache[mode] = import(`brace/mode/${mode}`).then(() => {
cache[mode] = 'fullfilled'
cache[mode] = 'fulfilled'
})
throw cache[mode]
}
Expand Down

0 comments on commit cac80b8

Please sign in to comment.