Skip to content

Commit

Permalink
test: Add some tests for makeMetadataQualification method
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Dec 16, 2024
1 parent a8a0d1b commit ee751fd
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 16 deletions.
60 changes: 45 additions & 15 deletions docs/api/cozy-client/modules/models.paper.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

*Defined in*

[packages/cozy-client/src/models/paper.js:276](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L276)
[packages/cozy-client/src/models/paper.js:280](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L280)

## Variables

Expand Down Expand Up @@ -156,7 +156,7 @@ Formatted and translated value of an array of contact

*Defined in*

[packages/cozy-client/src/models/paper.js:448](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L448)
[packages/cozy-client/src/models/paper.js:452](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L452)

***

Expand All @@ -181,7 +181,7 @@ Formatted and translated value for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:326](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L326)
[packages/cozy-client/src/models/paper.js:330](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L330)

***

Expand All @@ -207,7 +207,7 @@ Formatted and translated value for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:369](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L369)
[packages/cozy-client/src/models/paper.js:373](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L373)

***

Expand All @@ -231,7 +231,7 @@ Formatted and translated value for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:245](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L245)
[packages/cozy-client/src/models/paper.js:249](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L249)

***

Expand All @@ -256,7 +256,7 @@ Formatted and translated value for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:423](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L423)
[packages/cozy-client/src/models/paper.js:427](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L427)

***

Expand All @@ -280,7 +280,7 @@ The type of the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:284](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L284)
[packages/cozy-client/src/models/paper.js:288](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L288)

***

Expand All @@ -303,7 +303,7 @@ Translated name for contact

*Defined in*

[packages/cozy-client/src/models/paper.js:438](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L438)
[packages/cozy-client/src/models/paper.js:442](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L442)

***

Expand All @@ -327,7 +327,7 @@ Translated name for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:313](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L313)
[packages/cozy-client/src/models/paper.js:317](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L317)

***

Expand All @@ -352,7 +352,7 @@ Translated name for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:346](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L346)
[packages/cozy-client/src/models/paper.js:350](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L350)

***

Expand All @@ -376,7 +376,7 @@ Translated name for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:410](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L410)
[packages/cozy-client/src/models/paper.js:414](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L414)

***

Expand Down Expand Up @@ -462,7 +462,7 @@ Translated name for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:501](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L501)
[packages/cozy-client/src/models/paper.js:505](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L505)

***

Expand All @@ -484,7 +484,7 @@ Translated name for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:487](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L487)
[packages/cozy-client/src/models/paper.js:491](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L491)

***

Expand All @@ -505,7 +505,7 @@ Translated name for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:459](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L459)
[packages/cozy-client/src/models/paper.js:463](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L463)

***

Expand All @@ -527,4 +527,34 @@ Translated name for the metadata

*Defined in*

[packages/cozy-client/src/models/paper.js:471](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L471)
[packages/cozy-client/src/models/paper.js:475](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L475)

***

### makeMetadataQualification

**makeMetadataQualification**(`params`): `Object`

*Parameters*

| Name | Type | Description |
| :------ | :------ | :------ |
| `params` | `Object` | - |
| `params.knownMetadataPath` | `string` | Path of the metadata |
| `params.metadata` | `any` | An io.cozy.files metadata object |
| `params.value` | `string` | - |

*Returns*

`Object`

displayable metadata

| Name | Type |
| :------ | :------ |
| `name` | `string` |
| `value` | `string` |

*Defined in*

[packages/cozy-client/src/models/paper.js:224](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/paper.js#L224)
6 changes: 5 additions & 1 deletion packages/cozy-client/src/models/paper.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ export const isExpiringSoon = file => {
* @param {string | null} [params.value] - Value of the metadata
* @returns {{ name: string, value: string | null }} displayable metadata
*/
const makeMetadataQualification = ({ metadata, knownMetadataPath, value }) => {
export const makeMetadataQualification = ({
metadata,
knownMetadataPath,
value
}) => {
const _value = value || get(metadata, knownMetadataPath, null) || null
const shouldReturnThisMetadata = Object.keys(metadata).some(val =>
knownMetadataPath.startsWith(val)
Expand Down
60 changes: 60 additions & 0 deletions packages/cozy-client/src/models/paper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,66 @@ describe('Expiration', () => {
)
})

describe('makeMetadataQualification', () => {
it('should return formatted metadata', () => {
const fakeMetadata = {
number: '111111',
AObtentionDate: '2029-12-01T23:00:00.000Z',
expirationDate: '2029-12-05T23:00:00.000Z'
}

const res = paperModel.makeMetadataQualification({
metadata: fakeMetadata,
knownMetadataPath: 'expirationDate'
})

expect(res).toEqual({
name: 'expirationDate',
value: '2029-12-05T23:00:00.000Z'
})
})

it('should return formatted metadata only if matching label', () => {
const fakeMetadata = {
number: '111111',
AObtentionDate: '2029-12-01T23:00:00.000Z',
BObtentionDate: '2029-12-02T23:00:00.000Z',
CObtentionDate: '2029-12-03T23:00:00.000Z',
expirationDate: '2029-12-05T23:00:00.000Z',
referencedDate: '2029-12-06T23:00:00.000Z',
issueDate: '2029-12-07T23:00:00.000Z',
shootingDate: '2029-12-08T23:00:00.000Z',
date: '2029-12-09T23:00:00.000Z',
datetime: '2029-12-10T23:00:00.000Z',
qualification: { label: 'fake_label' },
page: 'front',
contact: 'Alice Durand',
noticePeriod: ''
}

const metadataLabels = [
'AObtentionDate',
'BObtentionDate',
'CObtentionDate',
'DObtentionDate'
]

const res = metadataLabels.map(dateName =>
paperModel.makeMetadataQualification({
metadata: fakeMetadata,
knownMetadataPath: dateName
})
)

expect(res).toEqual([
{ name: 'AObtentionDate', value: '2029-12-01T23:00:00.000Z' },
{ name: 'BObtentionDate', value: '2029-12-02T23:00:00.000Z' },
{ name: 'CObtentionDate', value: '2029-12-03T23:00:00.000Z' },
null
])
})
})

describe('formatMetadataQualification', () => {
it('should return correctly formatted metadata', () => {
const fakeMetadata = {
Expand Down
8 changes: 8 additions & 0 deletions packages/cozy-client/types/models/paper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export function computeExpirationNoticeDate(file: IOCozyFile): Date | null;
export function computeExpirationNoticeLink(file: IOCozyFile): string | null;
export function isExpired(file: IOCozyFile): boolean;
export function isExpiringSoon(file: IOCozyFile): boolean;
export function makeMetadataQualification({ metadata, knownMetadataPath, value }: {
metadata: any;
knownMetadataPath: string;
value: string | null;
}): {
name: string;
value: string | null;
};
export function formatMetadataQualification(metadata: any): {
name: string;
value: string | null;
Expand Down

0 comments on commit ee751fd

Please sign in to comment.