This commit is contained in:
Pig Fang 2020-02-07 15:21:43 +08:00
parent 2d57248654
commit 55f9ba1cff
2 changed files with 51 additions and 49 deletions

View File

@ -1,9 +1,9 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect } from 'react'
import { hot } from 'react-hot-loader/root' import { hot } from 'react-hot-loader/root'
import { trans } from '../../../scripts/i18n' import { t } from '@/scripts/i18n'
import * as fetch from '../../../scripts/net' import * as fetch from '@/scripts/net'
import { showModal, toast } from '../../../scripts/notify' import { showModal, toast } from '@/scripts/notify'
import Loading from '../../../components/Loading' import Loading from '@/components/Loading'
import Row from './Row' import Row from './Row'
import ModalCreate from './ModalCreate' import ModalCreate from './ModalCreate'
import { App } from './types' import { App } from './types'
@ -46,11 +46,12 @@ const OAuth: React.FC = () => {
const editName = async (app: App, index: number) => { const editName = async (app: App, index: number) => {
let name: string let name: string
try { try {
;({ value: name } = await showModal({ const { value } = await showModal({
mode: 'prompt', mode: 'prompt',
title: trans('user.oauth.name'), title: t('user.oauth.name'),
input: app.name, input: app.name,
})) })
name = value
} catch { } catch {
return return
} }
@ -72,11 +73,12 @@ const OAuth: React.FC = () => {
const editRedirect = async (app: App, index: number) => { const editRedirect = async (app: App, index: number) => {
let redirect: string let redirect: string
try { try {
;({ value: redirect } = await showModal({ const { value } = await showModal({
mode: 'prompt', mode: 'prompt',
title: trans('user.oauth.redirect'), title: t('user.oauth.redirect'),
input: app.redirect, input: app.redirect,
})) })
redirect = value
} catch { } catch {
return return
} }
@ -98,7 +100,7 @@ const OAuth: React.FC = () => {
const handleDelete = async (app: App) => { const handleDelete = async (app: App) => {
try { try {
await showModal({ await showModal({
text: trans('user.oauth.confirmRemove'), text: t('user.oauth.confirmRemove'),
okButtonType: 'danger', okButtonType: 'danger',
}) })
} catch { } catch {
@ -112,18 +114,18 @@ const OAuth: React.FC = () => {
return ( return (
<> <>
<button className="btn btn-primary" onClick={handleShowModalCreate}> <button className="btn btn-primary" onClick={handleShowModalCreate}>
{trans('user.oauth.create')} {t('user.oauth.create')}
</button> </button>
<div className="card mt-2"> <div className="card mt-2">
<div className="card-body p-0"> <div className="card-body p-0">
<table className="table table-striped"> <table className="table table-striped">
<thead> <thead>
<tr> <tr>
<th>{trans('user.oauth.id')}</th> <th>{t('user.oauth.id')}</th>
<th>{trans('user.oauth.name')}</th> <th>{t('user.oauth.name')}</th>
<th>{trans('user.oauth.secret')}</th> <th>{t('user.oauth.secret')}</th>
<th>{trans('user.oauth.redirect')}</th> <th>{t('user.oauth.redirect')}</th>
<th>{trans('admin.operationsTitle')}</th> <th>{t('admin.operationsTitle')}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>

View File

@ -1,7 +1,7 @@
import React from 'react' import React from 'react'
import { render, fireEvent, wait } from '@testing-library/react' import { render, fireEvent, wait } from '@testing-library/react'
import * as fetch from '@/scripts/net' import * as fetch from '@/scripts/net'
import { trans } from '@/scripts/i18n' import { t } from '@/scripts/i18n'
import OAuth from '@/views/user/OAuth' import OAuth from '@/views/user/OAuth'
import { App } from '@/views/user/OAuth/types' import { App } from '@/views/user/OAuth/types'
@ -30,14 +30,14 @@ describe('create app', () => {
const { getByPlaceholderText, getByText, queryByText } = render(<OAuth />) const { getByPlaceholderText, getByText, queryByText } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByText(trans('user.oauth.create'))) fireEvent.click(getByText(t('user.oauth.create')))
fireEvent.input(getByPlaceholderText(trans('user.oauth.name')), { fireEvent.input(getByPlaceholderText(t('user.oauth.name')), {
target: { value: 'My App' }, target: { value: 'My App' },
}) })
fireEvent.input(getByPlaceholderText(trans('user.oauth.redirect')), { fireEvent.input(getByPlaceholderText(t('user.oauth.redirect')), {
target: { value: 'http://url.test/' }, target: { value: 'http://url.test/' },
}) })
fireEvent.click(getByText(trans('general.confirm'))) fireEvent.click(getByText(t('general.confirm')))
await wait() await wait()
expect(fetch.post).toBeCalledWith('/oauth/clients', { expect(fetch.post).toBeCalledWith('/oauth/clients', {
@ -57,14 +57,14 @@ describe('create app', () => {
) )
await wait() await wait()
fireEvent.click(getByText(trans('user.oauth.create'))) fireEvent.click(getByText(t('user.oauth.create')))
fireEvent.input(getByPlaceholderText(trans('user.oauth.name')), { fireEvent.input(getByPlaceholderText(t('user.oauth.name')), {
target: { value: 'My App' }, target: { value: 'My App' },
}) })
fireEvent.input(getByPlaceholderText(trans('user.oauth.redirect')), { fireEvent.input(getByPlaceholderText(t('user.oauth.redirect')), {
target: { value: 'http://url.test/' }, target: { value: 'http://url.test/' },
}) })
fireEvent.click(getByText(trans('general.confirm'))) fireEvent.click(getByText(t('general.confirm')))
await wait() await wait()
expect(fetch.post).toBeCalledWith('/oauth/clients', { expect(fetch.post).toBeCalledWith('/oauth/clients', {
@ -81,21 +81,21 @@ describe('create app', () => {
const { getByPlaceholderText, getByText } = render(<OAuth />) const { getByPlaceholderText, getByText } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByText(trans('user.oauth.create'))) fireEvent.click(getByText(t('user.oauth.create')))
fireEvent.input(getByPlaceholderText(trans('user.oauth.name')), { fireEvent.input(getByPlaceholderText(t('user.oauth.name')), {
target: { value: 'My App' }, target: { value: 'My App' },
}) })
fireEvent.input(getByPlaceholderText(trans('user.oauth.redirect')), { fireEvent.input(getByPlaceholderText(t('user.oauth.redirect')), {
target: { value: 'http://url.test/' }, target: { value: 'http://url.test/' },
}) })
fireEvent.click(getByText(trans('general.cancel'))) fireEvent.click(getByText(t('general.cancel')))
await wait() await wait()
expect(fetch.post).not.toBeCalled() expect(fetch.post).not.toBeCalled()
fireEvent.click(getByText(trans('user.oauth.create'))) fireEvent.click(getByText(t('user.oauth.create')))
expect(getByPlaceholderText(trans('user.oauth.name'))).toHaveValue('') expect(getByPlaceholderText(t('user.oauth.name'))).toHaveValue('')
expect(getByPlaceholderText(trans('user.oauth.redirect'))).toHaveValue('') expect(getByPlaceholderText(t('user.oauth.redirect'))).toHaveValue('')
}) })
}) })
@ -113,11 +113,11 @@ describe('edit app', () => {
) )
await wait() await wait()
fireEvent.click(getByTitle(trans('user.oauth.modifyName'))) fireEvent.click(getByTitle(t('user.oauth.modifyName')))
fireEvent.input(getByDisplayValue(fixture.name), { fireEvent.input(getByDisplayValue(fixture.name), {
target: { value: 'new name' }, target: { value: 'new name' },
}) })
fireEvent.click(getByText(trans('general.confirm'))) fireEvent.click(getByText(t('general.confirm')))
await wait() await wait()
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, { expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
@ -139,11 +139,11 @@ describe('edit app', () => {
} = render(<OAuth />) } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByTitle(trans('user.oauth.modifyName'))) fireEvent.click(getByTitle(t('user.oauth.modifyName')))
fireEvent.input(getByDisplayValue(fixture.name), { fireEvent.input(getByDisplayValue(fixture.name), {
target: { value: 'new name' }, target: { value: 'new name' },
}) })
fireEvent.click(getByText(trans('general.confirm'))) fireEvent.click(getByText(t('general.confirm')))
await wait() await wait()
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, { expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
@ -159,8 +159,8 @@ describe('edit app', () => {
const { getByTitle, getByText, queryByText } = render(<OAuth />) const { getByTitle, getByText, queryByText } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByTitle(trans('user.oauth.modifyName'))) fireEvent.click(getByTitle(t('user.oauth.modifyName')))
fireEvent.click(getByText(trans('general.cancel'))) fireEvent.click(getByText(t('general.cancel')))
await wait() await wait()
expect(fetch.put).not.toBeCalled() expect(fetch.put).not.toBeCalled()
@ -177,11 +177,11 @@ describe('edit app', () => {
) )
await wait() await wait()
fireEvent.click(getByTitle(trans('user.oauth.modifyUrl'))) fireEvent.click(getByTitle(t('user.oauth.modifyUrl')))
fireEvent.input(getByDisplayValue(fixture.redirect), { fireEvent.input(getByDisplayValue(fixture.redirect), {
target: { value: 'http://new.test/' }, target: { value: 'http://new.test/' },
}) })
fireEvent.click(getByText(trans('general.confirm'))) fireEvent.click(getByText(t('general.confirm')))
await wait() await wait()
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, { expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
@ -203,11 +203,11 @@ describe('edit app', () => {
} = render(<OAuth />) } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByTitle(trans('user.oauth.modifyUrl'))) fireEvent.click(getByTitle(t('user.oauth.modifyUrl')))
fireEvent.input(getByDisplayValue(fixture.redirect), { fireEvent.input(getByDisplayValue(fixture.redirect), {
target: { value: 'http://new.test/' }, target: { value: 'http://new.test/' },
}) })
fireEvent.click(getByText(trans('general.confirm'))) fireEvent.click(getByText(t('general.confirm')))
await wait() await wait()
expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, { expect(fetch.put).toBeCalledWith(`/oauth/clients/${fixture.id}`, {
@ -223,8 +223,8 @@ describe('edit app', () => {
const { getByTitle, getByText, queryByText } = render(<OAuth />) const { getByTitle, getByText, queryByText } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByTitle(trans('user.oauth.modifyUrl'))) fireEvent.click(getByTitle(t('user.oauth.modifyUrl')))
fireEvent.click(getByText(trans('general.cancel'))) fireEvent.click(getByText(t('general.cancel')))
await wait() await wait()
expect(fetch.put).not.toBeCalled() expect(fetch.put).not.toBeCalled()
@ -242,8 +242,8 @@ describe('delete app', () => {
const { getByText, queryByText } = render(<OAuth />) const { getByText, queryByText } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByText(trans('report.delete'))) fireEvent.click(getByText(t('report.delete')))
fireEvent.click(getByText(trans('general.confirm'))) fireEvent.click(getByText(t('general.confirm')))
await wait() await wait()
expect(fetch.del).toBeCalledWith(`/oauth/clients/${fixture.id}`) expect(fetch.del).toBeCalledWith(`/oauth/clients/${fixture.id}`)
@ -255,8 +255,8 @@ describe('delete app', () => {
const { getByText, queryByText } = render(<OAuth />) const { getByText, queryByText } = render(<OAuth />)
await wait() await wait()
fireEvent.click(getByText(trans('report.delete'))) fireEvent.click(getByText(t('report.delete')))
fireEvent.click(getByText(trans('general.cancel'))) fireEvent.click(getByText(t('general.cancel')))
await wait() await wait()
expect(fetch.post).not.toBeCalled() expect(fetch.post).not.toBeCalled()