Skip to content

Commit

Permalink
feat(admin-ui): ui & client component tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
jv18creator committed Jul 27, 2023
1 parent 962d6c0 commit d721edc
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 28 deletions.
2 changes: 1 addition & 1 deletion admin-ui/app/components/CustomInput/CustomInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const CustomInput = (props) => {
})

return (
<RSCustomInput className={ inputClass } { ...otherProps } />
<RSCustomInput data-testid={props.name} className={ inputClass } { ...otherProps } />
)
}
CustomInput.propTypes = { ...RSCustomInput.propTypes }
Expand Down
1 change: 0 additions & 1 deletion admin-ui/app/routes/Apps/Gluu/GluuSelectRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react'
import GluuLabel from './GluuLabel'
import { Col, FormGroup, CustomInput, InputGroup } from 'Components'
import { useTranslation } from 'react-i18next'
import GluuTooltip from './GluuTooltip'

function GluuSelectRow({
label,
Expand Down
2 changes: 0 additions & 2 deletions admin-ui/app/routes/Apps/Gluu/GluuToogle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ function GluuToogle({ name, formik, value, handler, disabled }) {
<Toggle
name={name}
data-testid={name}
defaultChecked={value}
onClick={handler}
onChange={(event) => {
setChecked(event.target.checked)
if(formik !== undefined) {
Expand Down
9 changes: 5 additions & 4 deletions admin-ui/app/routes/Apps/Gluu/GluuTypeAheadWithAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState, useContext } from 'react'
import { FormGroup, Col, Row, Button, Input } from 'Components'
import { Typeahead } from 'react-bootstrap-typeahead'
import GluuLabel from '../Gluu/GluuLabel'
import GluuTooltip from './GluuTooltip'
import applicationStyle from './styles/applicationstyle'
import { useTranslation } from 'react-i18next'
import { ThemeContext } from 'Context/theme/themeContext'
Expand Down Expand Up @@ -31,10 +30,11 @@ function GluuTypeAheadWithAdd({
const newItem = document.getElementById(inputId).value
document.getElementById(inputId).value = ''
if (validator(newItem)) {
items.push(newItem)
const updatedItems = [...items]
updatedItems.push(newItem)
opts.push(newItem)
setItems(items)
formik.setFieldValue(name, items)
setItems(updatedItems)
formik.setFieldValue(name, updatedItems)
}
}

Expand Down Expand Up @@ -74,6 +74,7 @@ function GluuTypeAheadWithAdd({
disabled={disabled}
style={applicationStyle.buttonStyle}
onClick={addItem}
data-testid={t('actions.add')}
>
<i className="fa fa-plus-circle me-2"></i>
{t('actions.add')}
Expand Down
80 changes: 80 additions & 0 deletions admin-ui/app/routes/Apps/Gluu/Tests/GluuSelectRow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { fireEvent, screen } from '@testing-library/dom'
import GluuSelectRow from '../GluuSelectRow'
import { render } from '@testing-library/react'

const label = 'Select Option:'
const name = 'selectName'
const value = 'option2'
const values = ['option1', 'option2', 'option3']

describe('GluuSelectRow', () => {
// Mock the formik object and its handleChange method
const formikHandleChangeMock = jest.fn()
const formikMock = {
handleChange: formikHandleChangeMock,
}

test('renders select with formik handle change & update value', () => {
render(
<GluuSelectRow
label={label}
name={name}
value={value}
values={values}
formik={formikMock}
/>
)

const selectElement = screen.getByTestId(name)

expect(selectElement.value).toBe(value)

const newValue = 'option3'
fireEvent.change(selectElement, { target: { value: newValue } })

// Ensure that the handleChange method from formikMock was called with the correct arguments
expect(formikHandleChangeMock).toHaveBeenCalledTimes(1)
expect(formikHandleChangeMock).toHaveBeenCalledWith(
expect.objectContaining({
target: expect.objectContaining({
name,
value: newValue,
}),
})
)
})

test('renders select with prop handle change method & update value', () => {
// Mock handleChange method
const handleChangeMock = jest.fn()

render(
<GluuSelectRow
label={label}
name={name}
value={value}
values={values}
formik={formikMock}
handleChange={handleChangeMock}
/>
)

const selectElement = screen.getByTestId(name)

expect(selectElement.value).toBe(value)

const newValue = 'option1'

// Ensure that the props handleChange method was called
fireEvent.change(selectElement, { target: { value: newValue } })

// Retrieve the event
const eventArgument = handleChangeMock.mock.calls[0][0]

expect(handleChangeMock).toHaveBeenCalledTimes(1)
expect(handleChangeMock).toHaveBeenCalledWith(eventArgument)

// updates with new value option1
expect(selectElement.value).toBe(newValue)
})
})
32 changes: 32 additions & 0 deletions admin-ui/app/routes/Apps/Gluu/Tests/GluuToggle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { fireEvent, screen } from '@testing-library/dom'
import GluuToogle from '../GluuToogle'
import { render } from '@testing-library/react'

describe('Toggle switch', () => {
it('should update the state and UI when toggled', () => {
// Set up initial values
const initialChecked = true
const name = 'switch'
const handlerMock = jest.fn() // Create a mock function for the handler

// Render the component with initial props and state
render(
<GluuToogle name={name} value={initialChecked} handler={handlerMock} />
)

// Find the Toggle component in the DOM
const toggleElement = screen.getByTestId(name)

// Assert that the Toggle is initially checked
expect(toggleElement.checked).toBe(initialChecked)

// Simulate a click on the Toggle
fireEvent.click(toggleElement)

// Assert that the handler function was called
expect(handlerMock).toHaveBeenCalledTimes(1)

// Assert that the state has been updated correctly after the click
expect(toggleElement.checked).toBe(!initialChecked)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,11 @@ describe('OIDC Client CRUD Operation', () => {
})

it('GET OIDC client list', async () => {
log(`initialState`, initialState)
const result = await expectSaga(getOauthOpenidClients, {
payload: { action: {} },
})
.withReducer(rootReducer, initialState)
.run(false)
log(`result.returnValue`, result.returnValue)
expect(Array.isArray(result.returnValue.entries)).toBe(true)
expect(result.returnValue instanceof Error).toBe(false)
})
Expand Down Expand Up @@ -171,16 +169,4 @@ describe('OIDC Client CRUD Operation', () => {

expect(result.returnValue instanceof Error).toBe(false)
})

it('test initAudit return value', () => {
expectSaga(initAudit)
.withReducer(oidcReducer, initialState)
.returns({
client_id: '',
ip_address: '',
author: undefined,
status: 'succeed',
})
.run()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@ import ClientDetailPage from 'Plugins/auth-server/components/Clients/ClientDetai
import clients from './clients.test'
import AppTestWrapper from 'Routes/Apps/Gluu/Tests/Components/AppTestWrapper.test'

const Wrapper = ({ children }) => (
<AppTestWrapper>{children}</AppTestWrapper>
)
const Wrapper = ({ children }) => <AppTestWrapper>{children}</AppTestWrapper>
const permissions = [
'https://jans.io/oauth/config/openid/clients.readonly',
'https://jans.io/oauth/config/openid/clients.write',
'https://jans.io/oauth/config/openid/clients.delete',
]
const row = clients[0]
it('Should the client detail page properly properly', () => {
it('Should show client details properly', () => {
render(<ClientDetailPage row={row} scopes={permissions} />, {
wrapper: Wrapper,
})
screen.getByText(/1801.a0beec01-617b-4607-8a35-3e46ac43deb5/)
screen.getByText('Jans Config Api Client')
screen.getByText('pairwise')
screen.getByText(/Name/)

expect(screen.getByText(/Scopes/i)).toBeInTheDocument()
expect(screen.getByText(/Client Id/i)).toBeInTheDocument()
expect(screen.getByText(/Description/i)).toBeInTheDocument()
expect(screen.getByText(/Application type/i)).toBeInTheDocument()
expect(screen.getByText(/Status/i)).toBeInTheDocument()
expect(screen.getByText(/Grants/i)).toBeInTheDocument()
expect(screen.getByText(/Login URIs/i)).toBeInTheDocument()
expect(
screen.getByText(/Authentication method for the Token Endpoint/i)
).toBeInTheDocument()
expect(screen.getByText(/Logout Redirect URIs/i)).toBeInTheDocument()
expect(screen.getByText(/Response types/i)).toBeInTheDocument()
expect(screen.getByText(/Supress authorization/i)).toBeInTheDocument()
expect(screen.getByText(/id_token subject type/i)).toBeInTheDocument()
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import ClientWizardForm from 'Plugins/auth-server/components/Clients/ClientWizardForm'
import AppTestWrapper from 'Routes/Apps/Gluu/Tests/Components/AppTestWrapper.test'
import { Provider } from 'react-redux'
import { combineReducers, configureStore } from '@reduxjs/toolkit'
import clients from './clients.test'
import { reducer as scopeReducer } from 'Plugins/auth-server/redux/features/scopeSlice'
import oidcDiscoveryReducer from 'Redux/features/oidcDiscoverySlice'
import { t } from 'i18next'

const initReducer = {
scripts: [],
}

const store = configureStore({
reducer: combineReducers({
initReducer: (state = initReducer) => state,
scopeReducer: scopeReducer,
oidcDiscoveryReducer: oidcDiscoveryReducer,
}),
})

const Wrapper = ({ children }) => (
<AppTestWrapper>
<Provider store={store}>{children}</Provider>
</AppTestWrapper>
)

function hasInputValue(e, inputValue) {
return screen.getByDisplayValue(inputValue) === e
}

describe('Should render client add/edit form properly', () => {
test('render and update input fields', async () => {
const { container } = render(
<ClientWizardForm scripts={[]} client_data={clients[0]} />,
{
wrapper: Wrapper,
}
)
screen.debug(container, Infinity)
const clientNameInput = screen.getByTestId('clientName')
fireEvent.change(clientNameInput, { target: { value: 'test' } })
expect(hasInputValue(clientNameInput, 'test')).toBe(true)

const applicationType = screen.getByTestId('applicationType')
fireEvent.change(applicationType, { target: { value: 'web' } })
expect(hasInputValue(applicationType, 'web')).toBe(true)

const redirectUris = screen.getByTestId('new_entry')
fireEvent.change(redirectUris, { target: { value: 'www.gluu.org' } })
const addButton = screen.getByTestId(t('actions.add'))
fireEvent.click(addButton)
screen.debug(await screen.findByText('www.gluu.org'), Infinity)

fireEvent.change(redirectUris, { target: { value: 'www.google.com' } })
fireEvent.click(addButton)
screen.debug(await screen.findByText('www.google.com'), Infinity)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const ClientBasicPanel = ({
'password',
'urn:ietf:params:oauth:grant-type:uma-ticket',
]
const tokenEndpointAuthMethod = !!oidcConfiguration.tokenEndpointAuthMethodsSupported
const tokenEndpointAuthMethod = !!oidcConfiguration?.tokenEndpointAuthMethodsSupported
? oidcConfiguration.tokenEndpointAuthMethodsSupported
: []
const responseTypes = ['code', 'token', 'id_token']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ const cachRefreshState = {
},
};

const ldapReducer = {
loading: false
}

const store = createStore(
combineReducers({
noReducer: (state = {}) => state,
cacheRefreshReducer: (state = cachRefreshState) => state,
ldapReducer: (state = ldapReducer) => state
})
);

Expand All @@ -52,7 +57,7 @@ it("Should render cache refresh management page properly", () => {
wrapper: Wrapper,
});

const cacheRefreshLink = screen.getByText(`${t("menus.cache_refresh")}`);
const cacheRefreshLink = screen.getByText(`${t("menus.configuration")}`);
expect(cacheRefreshLink).toBeInTheDocument();

const customerBackendKeyAttributesLink = screen.getByText(
Expand Down

0 comments on commit d721edc

Please sign in to comment.