Skip to content

Commit

Permalink
Fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
easafe committed Feb 23, 2020
1 parent fd0ebfd commit 7d164df
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"homepage": ".",
"scripts": {
"start": "react-app-rewired start",
"prebuild": "flow check && cross-env CI=true yarn test",
"check": "flow check && cross-env CI=true yarn test",
"build": "react-app-rewired build && yarn run static",
"test": "react-app-rewired test --env=jsdom",
"eject": "react-scripts eject",
Expand Down
12 changes: 11 additions & 1 deletion src/frontend/components/EditEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ export default function EditEvent(props: Props) {
}

function EventForm({ events, spinner }: *) {
const { eventCreateFieldSet, eventDraft } = events.state

if (eventDraft && eventDraft.RecordTypeId) {
events.setEventLayout(eventDraft.RecordTypeId)
} else {
events.setDefaultEventLayout()
}

const description = events.getEventDescription()
const layout = events.getEventLayout()

if (!description || !layout) {
return null
}
const { eventCreateFieldSet, eventDraft } = events.state

const initialValues = {
...getDefaultValues(description, layout, eventCreateFieldSet),
...eventDraft
Expand Down Expand Up @@ -170,6 +179,7 @@ function EventModal({
getReference={fieldName =>
events.getReference(fieldName, eventDraft && eventDraft.Id)
}
eventRecordTypeInfos={events.state.eventRecordTypeInfos}
layout={layout}
timezone={events.state.timezone}
/>
Expand Down
38 changes: 38 additions & 0 deletions src/frontend/components/EditEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Provider } from "unstated"
import Events from "../containers/Events"
import * as af from "../models/Account.testFixtures"
import * as clf from "../models/CustomLabel.testFixtures"
import * as ef from "../models/Event.testFixtures"
import {
eventCreateFieldSet,
eventDescription,
Expand Down Expand Up @@ -66,6 +67,43 @@ it("renders a form", async () => {
})
})

it("sets layout from default record type for new events", async () => {
const events = new Events(eventsOpts)
await prepopulate(events)
const wrapper = mount(<EditEvent />, events)
const recordType = events.state.eventRecordTypeInfos.find(
({ defaultRecordTypeMapping }) => defaultRecordTypeMapping
)
if (recordType) {
expect(events.state.eventLayout).toBe(
ef.eventLayouts[recordType.urls.layout]
)
} else {
throw new Error("expected to match layout")
}
})

it("sets layout from custom record type for saved events", async () => {
const events = new Events(eventsOpts)
const recordType = events.state.eventRecordTypeInfos.find(
({ recordTypeId }) => recordTypeId == "012000000000000AAA"
)
if (recordType) {
await events.setEventDraft({
...draft,
RecordTypeId: recordType.recordTypeId
})
await prepopulate(events)
const wrapper = mount(<EditEvent />, events)

expect(events.state.eventLayout).toBe(
ef.eventLayouts[recordType.urls.layout]
)
} else {
throw new Error("expected to match layout")
}
})

it("saves an event", async () => {
const events = new Events(eventsOpts)
await events.setEventDraft(draft)
Expand Down
76 changes: 56 additions & 20 deletions src/frontend/components/SObjectForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import * as React from "react"
import * as FS from "../models/FieldSet"
import { type Layout, getPicklistValues } from "../models/Layout"
import { type Record } from "../models/QueryResult"
import { type SObjectDescription } from "../models/SObjectDescription"
import {
type SObjectDescription,
type PickListValue
} from "../models/SObjectDescription"
import Checkbox from "./forms/Checkbox"
import { type RecordTypeInfo } from "../models/RecordType"
import Combobox from "./forms/Combobox"
import DateTime from "./forms/DateTime"
import { getErrorText } from "./i18n/errorMessages"
Expand All @@ -20,15 +24,19 @@ type Props = {
fieldSet: FS.FieldSet,
getReference?: (fieldName: string) => ?Record,
layout: Layout,
eventRecordTypeInfos: RecordTypeInfo[],
timezone: string
}

const recordTypeFieldName = "RecordTypeId"

export default function SObjectForm({
description,
errors,
fieldSet,
getReference,
layout,
eventRecordTypeInfos,
timezone
}: Props) {
return (
Expand All @@ -39,7 +47,8 @@ export default function SObjectForm({
description,
getReference,
layout,
timezone
timezone,
eventRecordTypeInfos
)}
</Form>
)
Expand All @@ -52,7 +61,8 @@ function inputsForFieldSet(
description: SObjectDescription,
getReference: ?(fieldName: string) => ?Record,
layout: Layout,
timezone: string
timezone: string,
eventRecordTypeInfos: RecordTypeInfo[]
): React.Node {
const inputs = []
for (let i = 0; i < fieldSet.length; i += 2) {
Expand All @@ -73,7 +83,8 @@ function inputsForFieldSet(
description,
getReference,
layout,
timezone
timezone,
eventRecordTypeInfos
)}
</div>
))}
Expand Down Expand Up @@ -122,7 +133,8 @@ function inputFor(
description: SObjectDescription,
getReference: ?(fieldName: string) => ?Record,
layout: Layout,
timezone: string
timezone: string,
eventRecordTypeInfos: RecordTypeInfo[]
): React.Node {
switch (type) {
case "boolean":
Expand Down Expand Up @@ -170,22 +182,28 @@ function inputFor(
)
case "picklist":
const values = getPicklistValues(description, layout, name) || []
return (
<FormElement
errorMessage={errorMessage}
label={label}
required={required}
>
<Field className="slds-select" component="select" name={name}>
{values.map(({ label, value }) => (
<option key={value} value={value}>
{label}
</option>
))}
</Field>
</FormElement>
)

return picklistFor({ label, name, required, type }, errorMessage, values)
case "reference":
if (name === recordTypeFieldName) {
const values = eventRecordTypeInfos
.filter(
({ active, available, master }) => active && available && !master
)
.map(({ recordTypeId, name, defaultRecordTypeMapping }) => ({
active: true,
defaultValue: defaultRecordTypeMapping,
label: name,
value: recordTypeId
}))

return picklistFor(
{ label, name, required, type },
errorMessage,
values
)
}

const record = getReference && getReference(name)
const href = record && hrefFromApiUrl(record.attributes.url)
const address = record &&
Expand Down Expand Up @@ -223,6 +241,24 @@ function inputFor(
}
}

function picklistFor(
{ label, name, required }: FS.Field,
errorMessage: ?string,
values: PickListValue[]
) {
return (
<FormElement errorMessage={errorMessage} label={label} required={required}>
<Field className="slds-select" component="select" name={name}>
{values.map(({ label, value }) => (
<option key={value} value={value}>
{label}
</option>
))}
</Field>
</FormElement>
)
}

function Address({ city, country, postalCode, state, street }: FS.Address) {
return (
<span>
Expand Down
41 changes: 41 additions & 0 deletions src/frontend/components/SObjectForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
offsiteEventLayout
} from "../models/Event.testFixtures"
import { delay, inputElement } from "../testHelpers"
import * as ef from "../models/Event.testFixtures"
import Combobox from "./forms/Combobox"
import DateTime from "./forms/DateTime"
import SObjectForm from "./SObjectForm"
Expand All @@ -38,6 +39,7 @@ afterEach(() => {
it("presents inputs based on a given field set", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{ name: "InputA", label: "Input A", type: "string" },
Expand All @@ -59,6 +61,7 @@ it("presents inputs based on a given field set", () => {
it("presents a checkbox input", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{
Expand All @@ -83,6 +86,7 @@ it("presents a checkbox input", () => {
it("presents a checked checkbox when the corresponding value is `true`", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{ name: "IsAllDayEvent", label: "Is All Day Event", type: "boolean" }
Expand All @@ -99,6 +103,7 @@ it("presents a checked checkbox when the corresponding value is `true`", () => {
it("presents a combobox input", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{
Expand Down Expand Up @@ -141,6 +146,7 @@ it("presents a combobox input", () => {
it("gets combobox values from the given layout", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{
Expand Down Expand Up @@ -176,6 +182,7 @@ it("gets combobox values from the given layout", () => {
it("displays validation error message with combobox input", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
errors={{ Subject: FIELD_REQUIRED }}
fieldSet={[
Expand Down Expand Up @@ -204,6 +211,7 @@ it("presents a picklist input", () => {
]
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[{ name: "ShowAs", label: "Show Time As", type: "picklist" }]}
layout={layout}
Expand All @@ -218,9 +226,35 @@ it("presents a picklist input", () => {
}
})

it("presents record types as a picklist input", () => {
// Values from events fixtures
const values = [
{ label: "First Event Record Type", value: "012f2000000lw2FAAQ" },
{ label: "Offsite Events", value: "012f2000000lw2PAAQ" }
]
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={ef.eventRecordTypeInfos}
description={description}
fieldSet={[
{ name: "RecordTypeId", label: "Record Type Id", type: "reference" }
]}
layout={layout}
timezone={timezone}
/>
)
const input = wrapper.find("select")
expect(input.closest("label").text()).toMatch("Record Type Id")
for (const { label, value } of values) {
const option = input.find(`option[value='${value}']`)
expect(option.text()).toBe(label)
}
})

it("gets a boolean value from a checkbox input", async () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={eventCreateFieldSet}
layout={layout}
Expand All @@ -241,6 +275,7 @@ it("gets a boolean value from a checkbox input", async () => {
it("presents a date input", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{ name: "Date", label: "Date", required: false, type: "date" }
Expand All @@ -261,6 +296,7 @@ it("presents a date input", () => {
it("presents a datetime input", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{
Expand All @@ -287,6 +323,7 @@ it("presents a datetime input", () => {
it("gets a Date value from a datetime input", async () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={eventCreateFieldSet}
layout={layout}
Expand All @@ -313,6 +350,7 @@ it("gets a Date value from a datetime input", async () => {
it("presents a textarea input", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={[
{ name: "Description", label: "Description", type: "textarea" }
Expand All @@ -328,6 +366,7 @@ it("presents a textarea input", () => {
it("gets a string value from a textarea input", async () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
fieldSet={eventCreateFieldSet}
layout={layout}
Expand All @@ -349,6 +388,7 @@ it("gets a string value from a textarea input", async () => {
it("displays an asterisk in labels for required fields", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
errors={{ Description: FIELD_REQUIRED }}
fieldSet={[
Expand All @@ -373,6 +413,7 @@ it("displays an asterisk in labels for required fields", () => {
it("displays validation errors with form inputs", () => {
const wrapper = mount(
<SObjectForm
eventRecordTypeInfos={[]}
description={description}
errors={{ Description: FIELD_REQUIRED }}
fieldSet={[
Expand Down
Loading

0 comments on commit 7d164df

Please sign in to comment.