-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [ASAP-398] - set date if entry has not been published yet * update test * add condition for existing cases
- Loading branch information
Showing
2 changed files
with
51 additions
and
18 deletions.
There are no files selected for viewing
21 changes: 15 additions & 6 deletions
21
packages/contentful-app-extensions/date-field-as-first-published-at/src/locations/Field.tsx
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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
import React, { useState } from 'react'; | ||
import React, { useState, useEffect } from 'react'; | ||
import { Text } from '@contentful/f36-components'; | ||
import { EntrySys, FieldExtensionSDK } from '@contentful/app-sdk'; | ||
import { useSDK } from '@contentful/react-apps-toolkit'; | ||
import { EntrySys, FieldAppSDK } from '@contentful/app-sdk'; | ||
import { useFieldValue, useSDK } from '@contentful/react-apps-toolkit'; | ||
|
||
const Field = () => { | ||
const sdk = useSDK<FieldExtensionSDK>(); | ||
const sdk = useSDK<FieldAppSDK>(); | ||
const [value, setValue] = useFieldValue(); | ||
|
||
const [field] = useState(sdk.field.getValue()); | ||
useEffect(() => { | ||
if (!sdk.entry.getSys().firstPublishedAt) { | ||
setValue(new Date().toISOString()); | ||
} | ||
|
||
return <Text>{field ?? sdk.entry.getSys().firstPublishedAt}</Text>; | ||
if (sdk.entry.getSys().firstPublishedAt && !value) { | ||
setValue(sdk.entry.getSys().firstPublishedAt); | ||
} | ||
}, []); | ||
|
||
return <Text>{value}</Text>; | ||
}; | ||
|
||
export default Field; |
48 changes: 36 additions & 12 deletions
48
...tentful-app-extensions/date-field-as-first-published-at/src/test/locations/Field.test.tsx
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 |
---|---|---|
@@ -1,36 +1,60 @@ | ||
import '@testing-library/jest-dom'; | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import Field from '../../locations/Field'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
|
||
import { useSDK } from '@contentful/react-apps-toolkit'; | ||
import { useSDK, useFieldValue } from '@contentful/react-apps-toolkit'; | ||
|
||
jest.mock('@contentful/react-apps-toolkit', () => ({ | ||
useSDK: jest.fn(), | ||
useFieldValue: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
describe('Field component', () => { | ||
it('renders the value when its available', () => { | ||
it('updates to current timestamp if entry has not been published', async () => { | ||
(useSDK as jest.Mock).mockImplementation(() => ({ | ||
field: { | ||
getValue: jest.fn(() => '2023-04-12T16:05:00.000Z'), | ||
entry: { | ||
getSys: () => ({}), | ||
}, | ||
})); | ||
(useFieldValue as jest.Mock).mockImplementation(() => { | ||
return useState('2024-01-01T08:08:00.000Z'); | ||
}); | ||
render(<Field />); | ||
|
||
expect(screen.getByText('2023-04-12T16:05:00.000Z')).toBeInTheDocument(); | ||
expect(screen.getByText(new Date().toISOString())).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders the value from the system firstPublishedAt when it is not available', () => { | ||
it('does not update to current timestamp if entry has been published', async () => { | ||
(useSDK as jest.Mock).mockImplementation(() => ({ | ||
field: { | ||
getValue: jest.fn(), | ||
entry: { | ||
getSys: () => ({ firstPublishedAt: '2024-03-01T08:08:00.000Z' }), | ||
}, | ||
})); | ||
(useFieldValue as jest.Mock).mockImplementation(() => { | ||
return useState('2024-01-01T08:08:00.000Z'); | ||
}); | ||
render(<Field />); | ||
expect(screen.getByText('2024-01-01T08:08:00.000Z')).toBeInTheDocument(); | ||
}); | ||
|
||
it('updates with firstPublishedAt if entry published but field not set', async () => { | ||
(useSDK as jest.Mock).mockImplementation(() => ({ | ||
entry: { | ||
getSys: () => ({ firstPublishedAt: '2020-10-11T16:05:00.000Z' }), | ||
getSys: () => ({ firstPublishedAt: '2024-03-01T08:08:00.000Z' }), | ||
}, | ||
})); | ||
(useFieldValue as jest.Mock).mockImplementation(() => { | ||
return useState(undefined); | ||
}); | ||
render(<Field />); | ||
expect(screen.getByText('2020-10-11T16:05:00.000Z')).toBeInTheDocument(); | ||
expect(screen.getByText('2024-03-01T08:08:00.000Z')).toBeInTheDocument(); | ||
}); | ||
}); |