Skip to content

Commit cccda05

Browse files
authored
Merge pull request #112 from UgnisSoftware/UGN-356
chore UGN-356 - add tests and update readme for new prop dateFormat
2 parents 8557eb3 + 336d44b commit cccda05

File tree

6 files changed

+48
-227
lines changed

6 files changed

+48
-227
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ import { ReactSpreadsheetImport, StepType } from "react-spreadsheet-import";
178178
/>
179179
```
180180

181+
### Dates and time
182+
183+
Excel stores dates and times as numbers - offsets from an epoch. When reading xlsx files SheetJS provides date formatting helpers.
184+
**Default date import format** is `yyyy-mm-dd`. This format can be changed using **dateFormat** property.
185+
186+
- **dateFormat** - can be used to format dates when importing sheet data.
187+
188+
Common date-time formats can be viewed [here](https://docs.sheetjs.com/docs/csf/features/dates/#date-and-time-number-formats).
189+
181190
### Other optional props
182191

183192
```tsx

package-lock.json

Lines changed: 0 additions & 222 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/steps/SelectHeaderStep/tests/SelectHeaderStep.test.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { StepType } from "../../UploadFlow"
1212
const MUTATED_HEADER = "mutated header"
1313
const CONTINUE_BUTTON = "Next"
1414
const ERROR_MESSAGE = "Something happened"
15+
const RAW_DATE = "2020-03-03"
16+
const FORMATTED_DATE = "2020/03/03"
1517

1618
test("Select header row and click next", async () => {
1719
const data = [
@@ -69,11 +71,11 @@ test("selectHeaderStepHook should be called after header is selected", async ()
6971
fireEvent.click(continueButton)
7072
await waitFor(() => {
7173
expect(selectHeaderStepHook).toBeCalledWith(
72-
["name", "age"],
74+
["name", "age", "date"],
7375
[
74-
["Josh", "2"],
75-
["Charlie", "3"],
76-
["Lena", "50"],
76+
["Josh", "2", "2020-03-03"],
77+
["Charlie", "3", "2010-04-04"],
78+
["Lena", "50", "1994-02-27"],
7779
],
7880
)
7981
})
@@ -134,3 +136,35 @@ test("Should show error toast if error is thrown in selectHeaderStepHook", async
134136
expect(errorToast).toBeInTheDocument()
135137
})
136138
})
139+
140+
test("dateFormat property should be applied to dates read from csv files", async () => {
141+
const file = new File([RAW_DATE], "test.csv", {
142+
type: "text/csv",
143+
})
144+
render(<ReactSpreadsheetImport {...mockRsiValues} dateFormat="yyyy/mm/dd" />)
145+
146+
const uploader = screen.getByTestId("rsi-dropzone")
147+
fireEvent.drop(uploader, {
148+
target: { files: [file] },
149+
})
150+
151+
const el = await screen.findByText(FORMATTED_DATE, undefined, { timeout: 5000 })
152+
expect(el).toBeInTheDocument()
153+
})
154+
155+
test("dateFormat property should be applied to dates read from xlsx files", async () => {
156+
render(<ReactSpreadsheetImport {...mockRsiValues} dateFormat="yyyy/mm/dd" />)
157+
const uploader = screen.getByTestId("rsi-dropzone")
158+
const data = readFileSync(__dirname + "/../../../../static/Workbook2.xlsx")
159+
fireEvent.drop(uploader, {
160+
target: {
161+
files: [
162+
new File([data], "testFile.xlsx", {
163+
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
164+
}),
165+
],
166+
},
167+
})
168+
const el = await screen.findByText(FORMATTED_DATE, undefined, { timeout: 10000 })
169+
expect(el).toBeInTheDocument()
170+
})

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export type RsiProps<T extends string> = {
3939
autoMapDistance?: number
4040
// Initial Step state to be rendered on load
4141
initialStepState?: StepState
42-
// Date format to use
42+
// Date format to use e.g. "yyyy-mm-dd hh:mm:ss", "m/d/yy h:mm", 'mmm-yy', etc.
4343
dateFormat?: string
4444
}
4545

static/Workbook1.xlsx

-24 Bytes
Binary file not shown.

static/Workbook2.xlsx

2.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)