mirror of
https://github.com/bs-community/blessing-skin-plugins.git
synced 2025-01-09 04:07:51 +08:00
[texture-description] add tests
This commit is contained in:
parent
11db18cab8
commit
9e1174c4bf
@ -1,5 +1,7 @@
|
||||
{
|
||||
"preset": "ts-jest",
|
||||
"resetMocks": true,
|
||||
"timers": "modern",
|
||||
"moduleNameMapper": {
|
||||
"blessing-skin": "<rootDir>/types.ts"
|
||||
},
|
||||
|
106
plugins/texture-description/assets/Description.test.ts
Normal file
106
plugins/texture-description/assets/Description.test.ts
Normal 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()
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user