-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #832 from ivoalmeida/unit-tests-useobcname-schema-…
…hook added unit tests for useObcNameSchema hook
- Loading branch information
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import * as selectors from '@odf/shared/selectors'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useObcNameSchema from './useObcNameSchema'; | ||
|
||
jest.mock('@odf/shared/selectors', () => ({ | ||
getName: jest.fn().mockReturnValue('test'), | ||
})); | ||
|
||
jest.mock('@odf/shared/hooks/useK8sList', () => ({ | ||
__esModule: true, | ||
useK8sList: () => [ | ||
[ | ||
{ | ||
metadata: { | ||
name: 'test', | ||
}, | ||
}, | ||
], | ||
true, | ||
undefined, | ||
], | ||
})); | ||
|
||
describe('useObcNameSchema tests', () => { | ||
it('should return error "Cannot be used before"', async () => { | ||
const obcName = 'test'; | ||
const expected = 'Cannot be used before'; | ||
const spy = jest.spyOn(selectors, 'getName'); | ||
const { result } = renderHook(() => useObcNameSchema()); | ||
await expect( | ||
result.current.obcNameSchema.validate({ | ||
obcName, | ||
}) | ||
).rejects.toThrow(expected); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return error "No more than 253 characters"', async () => { | ||
const obcName = new Array(254).fill('a').join(''); | ||
const expected = 'No more than 253 characters'; | ||
const spy = jest.spyOn(selectors, 'getName'); | ||
const { result } = renderHook(() => useObcNameSchema()); | ||
await expect( | ||
result.current.obcNameSchema.validate({ | ||
obcName, | ||
}) | ||
).rejects.toThrow(expected); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return error "Starts and ends with a lowercase letter or number"', async () => { | ||
const obcName = '.invalid-name-'; | ||
const expected = 'Starts and ends with a lowercase letter or number'; | ||
const spy = jest.spyOn(selectors, 'getName'); | ||
const { result } = renderHook(() => useObcNameSchema()); | ||
await expect( | ||
result.current.obcNameSchema.validate({ | ||
obcName, | ||
}) | ||
).rejects.toThrow(expected); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return error "Only lowercase letters, numbers, non-consecutive periods, or hyphens"', async () => { | ||
const obcName = 'invalid---name09'; | ||
const expected = | ||
'Only lowercase letters, numbers, non-consecutive periods, or hyphens'; | ||
const spy = jest.spyOn(selectors, 'getName'); | ||
const { result } = renderHook(() => useObcNameSchema()); | ||
await expect( | ||
result.current.obcNameSchema.validate({ | ||
obcName, | ||
}) | ||
).rejects.toThrow(expected); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should pass validation on empty obc name input', async () => { | ||
const obcName = ''; | ||
const expected = {}; | ||
const spy = jest.spyOn(selectors, 'getName'); | ||
const { result } = renderHook(() => useObcNameSchema()); | ||
const actual = await result.current.obcNameSchema.validate({ | ||
obcName, | ||
}); | ||
expect(actual).toMatchObject(expected); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should pass validation on valid obc name input', async () => { | ||
const obcName = 'valid-name'; | ||
const expected = { obcName }; | ||
const spy = jest.spyOn(selectors, 'getName'); | ||
const { result } = renderHook(() => useObcNameSchema()); | ||
const actual = await result.current.obcNameSchema.validate({ | ||
obcName, | ||
}); | ||
expect(actual).toMatchObject(expected); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should pass validation on max of 253 chars', async () => { | ||
const obcName = new Array(253).fill('a').join(''); | ||
const expected = { obcName }; | ||
const spy = jest.spyOn(selectors, 'getName'); | ||
const { result } = renderHook(() => useObcNameSchema()); | ||
const actual = await result.current.obcNameSchema.validate({ | ||
obcName, | ||
}); | ||
expect(actual).toMatchObject(expected); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
}); |