[texture-description] add tests

This commit is contained in:
Pig Fang 2020-08-31 09:08:31 +08:00
parent 11db18cab8
commit 9e1174c4bf
No known key found for this signature in database
GPG Key ID: A8198F548DADA9E2
2 changed files with 108 additions and 0 deletions

View File

@ -1,5 +1,7 @@
{
"preset": "ts-jest",
"resetMocks": true,
"timers": "modern",
"moduleNameMapper": {
"blessing-skin": "<rootDir>/types.ts"
},

View File

@ -0,0 +1,106 @@
import { render, fireEvent, waitFor } from '@testing-library/svelte'
import { tick } from 'svelte'
import { fetch, t } from 'blessing-skin'
import Description from './Description.svelte'
test('render description', async () => {
const spy = jest.spyOn(fetch, 'get').mockResolvedValue('<div id="md"></div>')
render(Description, { props: { tid: 1 } })
await waitFor(() => expect(spy).toBeCalledWith('/texture/1/description'))
await tick()
await tick()
expect(document.querySelector('#md')).toBeInTheDocument()
})
test('edit is not allowed', async () => {
const spy = jest.spyOn(fetch, 'get').mockResolvedValue('<div id="md"></div>')
const { queryByTitle } = render(Description, { props: { tid: 1 } })
await waitFor(() => expect(spy).toBeCalledWith('/texture/1/description'))
expect(queryByTitle(t('texture-description.edit'))).not.toBeInTheDocument()
})
describe('edit description', () => {
it('cancelled', async () => {
const spyGet = jest
.spyOn(fetch, 'get')
.mockResolvedValueOnce('<div id="md">a</div>')
.mockResolvedValueOnce('a')
const spyPut = jest.spyOn(fetch, 'put')
const { getByTitle, getByText } = render(Description, {
props: { tid: 1, canEdit: true },
})
await waitFor(() => expect(spyGet).toBeCalledWith('/texture/1/description'))
fireEvent.click(getByTitle(t('texture-description.edit')))
await waitFor(() =>
expect(spyGet).toBeCalledWith('/texture/1/description', { raw: true }),
)
await tick()
await tick()
fireEvent.click(getByText(t('general.cancel')))
await waitFor(() => expect(spyPut).not.toBeCalled())
})
it('max length exceeded', async () => {
const spy = jest
.spyOn(fetch, 'get')
.mockResolvedValueOnce('<div id="md">a</div>')
.mockResolvedValueOnce('a')
const { getByTitle, getByText, getByDisplayValue, queryByText } = render(
Description,
{
props: { tid: 1, canEdit: true, maxLength: 2 },
},
)
await waitFor(() => expect(spy).toBeCalledWith('/texture/1/description'))
fireEvent.click(getByTitle(t('texture-description.edit')))
await waitFor(() =>
expect(spy).toBeCalledWith('/texture/1/description', { raw: true }),
)
await tick()
await tick()
fireEvent.input(getByDisplayValue('a'), { target: { value: 'abcd' } })
await tick()
expect(
queryByText(t('texture-description.exceeded', { max: 2 })),
).toBeInTheDocument()
fireEvent.click(getByText(t('general.cancel')))
})
it('submit description', async () => {
const spyGet = jest
.spyOn(fetch, 'get')
.mockResolvedValueOnce('<div id="md">a</div>')
.mockResolvedValueOnce('a')
const spyPut = jest.spyOn(fetch, 'put').mockResolvedValue('<p>abcd</p>')
const { getByTitle, getByText, getByDisplayValue, queryByText } = render(
Description,
{
props: { tid: 1, canEdit: true },
},
)
await waitFor(() => expect(spyGet).toBeCalledWith('/texture/1/description'))
fireEvent.click(getByTitle(t('texture-description.edit')))
await waitFor(() =>
expect(spyGet).toBeCalledWith('/texture/1/description', { raw: true }),
)
await tick()
await tick()
fireEvent.input(getByDisplayValue('a'), { target: { value: 'abcd' } })
fireEvent.click(getByText(t('general.submit')))
await waitFor(() =>
expect(spyPut).toBeCalledWith('/texture/1/description', {
description: 'abcd',
}),
)
await tick()
await tick()
expect(queryByText('abcd')).toBeInTheDocument()
})
})