Unit Test
How do I set the values for fields in antform if getFieldsValue is used in the code?
Solution: use setFieldsValue to set value for fields in antform
test('import', (done: any) => { const testComponent = createRemoteImportForm() const form = testComponent.root.find( (node: any) => (node.type as any).name === 'RemoteImportForm', ) form.instance.props.form.setFieldsValue(getFieldsValueImport) setImmediate(() => { form.instance.handleImport() moxios.wait(() => { expect(form.instance.props.onClose.mock.calls.length).toBe(1) done() }) }) })
What is RDD and how do you write it?
A: RDD = readable driven development
Code:
describe("GIFW Table Test", () =>{ describe("Render the table", () =>{ it("Render Column A", () => {}); ..... it("Render Operation Actions", () => {}); }); describe("Operate the Table", () =>{ it("Click Delete Button", () => {}); ..... it("Search Filter.", () => {}); });});
Where are the documents for End-to-End and unit testing?
Timeout issue on SubGlobalServerList.text.tsx?
In the function 'onUnAssociateSG', serviceGroup API is /hccapi/v3/provider/${getItem( 'PROVIDER',)}/tenant/${tenant}/shared-object/slb/service-group/${sgName}
. Before testing the function, a provider name needs to be initialized. Otherwise, moxios API will not succeed, and a timeout error will be returned.
test('should call onUnassociateSg in PortList.tsx work', async (done: any) => {
;(global as any).sessionStorage.setItem('PROVIDER', 'root')
;(global as any).sessionStorage.setItem('tenant', 'test')
;(global as any).sessionStorage.setItem('ALLTENANTS', '[{"name": "test"}]')
mockGetServiceGroupResponse()
mockPostServiceGroupResponse()
const testComponent = renderPortList()
const portListNode = testComponent.root.find(
(node: any) => node.type.name === 'PortList',
)
await portListNode.instance.onUnAssociateSg(
portListProps.data[0],
'httpService1-80',
)
expect(testComponent.toJSON()).toMatchSnapshot()
done()
})
How do I test if the code has window.location.href?
1 Add a URL to the jest config
module.exports = { testURL: 'http://localhost/',
2 Use HTML5 history API
window.history.pushState({}, 'Test Title', '/cluster/xyz')
If you are using Redux state, ignoring A10Theme issue, and cannot run a10-gui-framework store procedure
1 createState
import { Map } from 'immutable'
import Settings from 'src/containers/Controller/Dashboard/Settings'
export default () => {
let data = Map()
data = data.setIn(Settings.namespace.dashboardEventInfo, Map(Settings.rangePeriod))
return {
A10Data: data,
A10Theme: Map(),
A10Locale: Map(),
}
}
2 Use initState={state} in HCProvider to add data to Redux store
const renderTestComponent = () => {
// Init Redux Store
const state = createState()
const ActivitiesList = require('../index').default
return TestRenderer.create(
<HCProvider initState={state>
<ActivitiesList parent="provider" />
</HCProvider>,
)
}
How do I prevent a 'Network Issue' error when running unit test code?
Use a try cacTh block for the source code.
How do I find your component in the customized form?
Solution: find it by name inside the moxios.wait function
1 add it into moxios.wait function
test('function handleItemChange', async () => {
mockAPIResponse()
const testComponent = createGeoListForm()
moxios.wait(() => {
const customForm = testComponent.root.find((node: any) => (node.type as any).name === 'GeoListForm')
let callback = jest.fn()
let item = { name: '' }
customForm.instance.handleItemChange('bj2', item, callback)
expect(item.name).toBe('bj2')
expect(callback.mock.calls.length).toBe(1)
})
})
2 add it into setImmediate function
test('function handleItemChange', (done: any) => {
mockAPIResponse()
const testComponent = createGeoListForm()
setImmediate(() => {
const customForm = testComponent.root.find((node: any) => (node.type as any).name === 'GeoListForm')
let callback = jest.fn()
let item = { name: '' }
customForm.instance.handleItemChange('bj2', item, callback)
expect(item.name).toBe('bj2')
expect(callback.mock.calls.length).toBe(1)
done()
})
})
Last updated
Was this helpful?