-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding test cases and code refactoing lint error correction lint error correction
- Loading branch information
1 parent
e7ecd42
commit 2751540
Showing
9 changed files
with
211 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** Function to append 0 to beginning of a number if it is a single digit. */ | ||
const padStart = (number) => String(number).padStart(2, '0'); | ||
|
||
/** This function format the date. */ | ||
export const formatDate = (date) => { | ||
const newDate = new Date(date[0]); | ||
return `${padStart(newDate.getMonth() + 1)}/${padStart(newDate.getDate())}/${newDate.getFullYear()}`; | ||
}; |
66 changes: 66 additions & 0 deletions
66
app/javascript/components/c-and-u-collections-form/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import MiqFormRenderer from '@@ddf'; | ||
import createSchema from './schema'; | ||
import NotificationMessage from '../notification-message'; | ||
import { timeZoneData } from '../schedule-form/helper'; | ||
import { formatDate } from './helper'; | ||
|
||
const DiagnosticsCURepairForm = () => { | ||
const [data, setData] = useState({ | ||
isLoading: true, | ||
timezones: undefined, | ||
response: undefined, | ||
}); | ||
|
||
useEffect(() => { | ||
API.get('/api').then(({ timezones }) => { | ||
setData({ | ||
...data, | ||
isLoading: false, | ||
timezones: timeZoneData(timezones), | ||
}); | ||
}); | ||
}, []); | ||
|
||
const onSubmit = (values) => { | ||
const paramsData = { | ||
timezone: values.timezone, | ||
start_date: formatDate(values.startDate), | ||
end_date: formatDate(values.endDate), | ||
}; | ||
http.post(`/ops/cu_repair?button=submit`, paramsData, { skipErrors: [400, 500] }) | ||
.then(({ status, message }) => { | ||
setData({ | ||
...data, | ||
isLoading: true, | ||
response: { status, message }, | ||
}); | ||
setTimeout(() => { | ||
setData({ | ||
...data, | ||
response: undefined, | ||
isLoading: false, | ||
}); | ||
}, 1000); | ||
}); | ||
}; | ||
|
||
const renderMessage = ({ status, message }) => <NotificationMessage type={status} message={message} />; | ||
|
||
return ( | ||
<> | ||
{ data.response && renderMessage(data.response) } | ||
{ !data.isLoading && ( | ||
<MiqFormRenderer | ||
schema={createSchema(data.timezones)} | ||
onSubmit={onSubmit} | ||
/> | ||
)} | ||
{ | ||
`${__('Note')} : ${__('Gap Collection is only available for VMware vSphere Infrastructures')}` | ||
} | ||
</> | ||
); | ||
}; | ||
|
||
export default DiagnosticsCURepairForm; |
36 changes: 36 additions & 0 deletions
36
app/javascript/components/c-and-u-collections-form/schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { componentTypes, validatorTypes } from '@@ddf'; | ||
|
||
const createSchema = (timezones) => ({ | ||
title: __('Collection Options'), | ||
fields: [ | ||
{ | ||
component: componentTypes.SELECT, | ||
id: 'timezone', | ||
name: 'timezone', | ||
label: __('Timezone'), | ||
initialValue: 'UTC', | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
isRequired: true, | ||
includeEmpty: true, | ||
options: timezones, | ||
}, | ||
{ | ||
component: 'date-picker', | ||
id: 'startDate', | ||
name: 'startDate', | ||
label: __('Start Date'), | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
isRequired: true, | ||
}, | ||
{ | ||
component: 'date-picker', | ||
id: 'endDate', | ||
name: 'endDate', | ||
label: __('End Date'), | ||
validate: [{ type: validatorTypes.REQUIRED }], | ||
isRequired: true, | ||
}, | ||
], | ||
}); | ||
|
||
export default createSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ascript/spec/c-and-u-collections-form/__snapshots__/c-and-u-collections-form.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`DiagnosticsCURepairForm Component Should add a record from DiagnosticsCURepair form 1`] = ` | ||
<Provider | ||
store={ | ||
Object { | ||
"asyncReducers": Object { | ||
"FormButtons": [Function], | ||
"notificationReducer": [Function], | ||
}, | ||
"dispatch": [Function], | ||
"getState": [Function], | ||
"injectReducers": [Function], | ||
"replaceReducer": [Function], | ||
"subscribe": [Function], | ||
Symbol(observable): [Function], | ||
} | ||
} | ||
> | ||
<DiagnosticsCURepairForm> | ||
Note : Gap Collection is only available for VMware vSphere Infrastructures | ||
</DiagnosticsCURepairForm> | ||
</Provider> | ||
`; | ||
|
||
exports[`DiagnosticsCURepairForm Component Should render a new DiagnosticsCURepair form 1`] = ` | ||
<Provider | ||
store={ | ||
Object { | ||
"asyncReducers": Object { | ||
"FormButtons": [Function], | ||
"notificationReducer": [Function], | ||
}, | ||
"dispatch": [Function], | ||
"getState": [Function], | ||
"injectReducers": [Function], | ||
"replaceReducer": [Function], | ||
"subscribe": [Function], | ||
Symbol(observable): [Function], | ||
} | ||
} | ||
> | ||
<DiagnosticsCURepairForm> | ||
Note : Gap Collection is only available for VMware vSphere Infrastructures | ||
</DiagnosticsCURepairForm> | ||
</Provider> | ||
`; |
43 changes: 43 additions & 0 deletions
43
app/javascript/spec/c-and-u-collections-form/c-and-u-collections-form.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import toJson from 'enzyme-to-json'; | ||
import fetchMock from 'fetch-mock'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { mount } from '../helpers/mountForm'; | ||
import DiagnosticsCURepairForm from '../../components/c-and-u-collections-form'; | ||
import { formatDate } from '../../components/c-and-u-collections-form/helper'; | ||
|
||
describe('DiagnosticsCURepairForm Component', () => { | ||
afterEach(() => { | ||
fetchMock.reset(); | ||
fetchMock.restore(); | ||
}); | ||
it('Should render a new DiagnosticsCURepair form', async() => { | ||
let wrapper; | ||
await act(async() => { | ||
wrapper = mount(<DiagnosticsCURepairForm />); | ||
}); | ||
wrapper.update(); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('Should add a record from DiagnosticsCURepair form', async(done) => { | ||
const paramsData = { | ||
timezone: 'UTC', | ||
start_date: formatDate('12/12/2023'), | ||
end_date: formatDate('12/01/2023'), | ||
}; | ||
const timezones = [ | ||
{ name: 'International Date Line West', description: '(GMT-12:00) International Date Line West' }, | ||
{ name: 'American Samoa', description: '(GMT-11:00) American Samoa' }, | ||
{ name: 'Midway Island', description: '(GMT-11:00) Midway Island' }, | ||
{ name: 'Hawaii', description: '(GMT-10:00) Hawaii' }, | ||
{ name: 'Alaska', description: '(GMT-09:00) Alaska' }, | ||
]; | ||
fetchMock.getOnce('/api', { timezones }); | ||
fetchMock.postOnce('/api/instances/1850//ops/cu_repair?button=submit/', paramsData); | ||
const wrapper = mount(<DiagnosticsCURepairForm />); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
done(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= react('DiagnosticsCURepairForm') =%> |