Skip to content

Commit c5f8504

Browse files
authored
Merge branch 'main' into SHAPE-5745-all-the-values-must-be-a-string-in-field-plugin-config-json
2 parents 978e380 + 5aa3c20 commit c5f8504

File tree

7 files changed

+72
-45
lines changed

7 files changed

+72
-45
lines changed

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginActions.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ModalChangeMessage,
66
ValueChangeMessage,
77
} from '../../messaging'
8+
import { emptyAsset } from '../../messaging/pluginMessage/containerToPluginMessage/Asset.test'
89

910
// INFO: The methods like `setContent` is not being resolved in this file because `pushCallback` doesn't resolve.
1011
// We can also mock `callbackQueue` and make it resolve, and resolve this `no-floating-promises` issue.
@@ -231,9 +232,10 @@ describe('createPluginActions', () => {
231232
field: 'dummy',
232233
callbackId: TEST_CALLBACK_ID,
233234
filename,
235+
asset: emptyAsset,
234236
})
235237
const result = await promise
236-
expect(result).toEqual({ filename })
238+
expect(result).toEqual(emptyAsset)
237239
})
238240
it('does not call the callack function when callbackId does not match', async () => {
239241
const WRONG_CALLBACK_ID = TEST_CALLBACK_ID + '_wrong'
@@ -255,6 +257,7 @@ describe('createPluginActions', () => {
255257
field: 'dummy',
256258
callbackId: WRONG_CALLBACK_ID,
257259
filename,
260+
asset: emptyAsset,
258261
})
259262
const resolvedFn = jest.fn()
260263
const rejectedFn = jest.fn()

packages/field-plugin/src/createFieldPlugin/createPluginActions/createPluginMessageListener/handlePluginMessage.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
LoadedMessage,
77
MessageToPlugin,
88
} from '../../../messaging'
9+
import { emptyAsset } from '../../../messaging/pluginMessage/containerToPluginMessage/Asset.test'
910

1011
const uid = 'abc123'
1112
const mockCallbacks = (): PluginMessageCallbacks => ({
@@ -95,6 +96,7 @@ describe('handlePluginMessage', () => {
9596
filename: '/my-file.jpg',
9697
field: 'callback-uid',
9798
callbackId: 'test-callback-id',
99+
asset: emptyAsset,
98100
}
99101
const callbacks = mockCallbacks()
100102
handlePluginMessage(data, uid, callbacks)

packages/field-plugin/src/messaging/pluginMessage/containerToPluginMessage/Asset.test.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
import { Asset, assetFromAssetSelectedMessage, isAsset } from './Asset'
1+
import {
2+
Asset,
3+
AssetWrapper,
4+
assetFromAssetSelectedMessage,
5+
isAsset,
6+
} from './Asset'
27
import { AssetSelectedMessage } from './AssetSelectedMessage'
38

4-
const stub: Asset = {
9+
export const emptyAsset: Asset = {
10+
id: 0,
11+
fieldtype: 'asset',
12+
name: '',
13+
filename: '',
14+
meta_data: {},
15+
title: '',
16+
copyright: '',
17+
focus: '',
18+
alt: '',
19+
source: '',
20+
is_private: false,
21+
}
22+
23+
const stub: AssetWrapper = {
524
filename: 'https://somthing.com/myimage.jpg',
25+
asset: emptyAsset,
626
}
727

828
const assetSelectedMessage: AssetSelectedMessage = {
@@ -11,6 +31,7 @@ const assetSelectedMessage: AssetSelectedMessage = {
1131
callbackId: 'test-callback-id',
1232
action: 'asset-selected',
1333
filename: 'https://somthing.com/myimage.jpg',
34+
asset: emptyAsset,
1435
}
1536

1637
describe('Asset', function () {
@@ -35,24 +56,8 @@ describe('Asset', function () {
3556
assetFromAssetSelectedMessage(assetSelectedMessage),
3657
).toHaveProperty('filename')
3758
})
38-
it('keeps unknown properties', () => {
39-
expect(
40-
assetFromAssetSelectedMessage({
41-
...assetSelectedMessage,
42-
unknownProperty: 'any-value',
43-
}),
44-
).toHaveProperty('unknownProperty', 'any-value')
45-
})
4659
})
4760
describe('validation', () => {
48-
it('allows unknown properties', () => {
49-
expect(
50-
isAsset({
51-
...stub,
52-
anUnknownProperty: 'something',
53-
}),
54-
).toEqual(true)
55-
})
5661
describe('the filename property', () => {
5762
it('is required', () => {
5863
expect(
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
import { hasKey } from '../../../utils'
22
import { AssetSelectedMessage } from './AssetSelectedMessage'
33

4+
export type AssetWrapper = {
5+
filename: string
6+
asset: Asset
7+
}
8+
49
export type Asset = {
10+
id: number
11+
fieldtype: 'asset'
512
filename: string
13+
name: string
14+
meta_data?: Record<string, unknown>
15+
title: string
16+
copyright: string
17+
focus: string
18+
alt: string
19+
source?: string
20+
is_external_url?: boolean
21+
is_private?: boolean
22+
src?: string
23+
updated_at?: string
624
}
725

8-
export const isAsset = (data: unknown): data is Asset =>
9-
hasKey(data, 'filename') && typeof data.filename === 'string'
26+
export const isAsset = (data: unknown): data is AssetWrapper =>
27+
hasKey(data, 'filename') &&
28+
typeof data.filename === 'string' &&
29+
hasKey(data, 'asset') &&
30+
typeof data.asset === 'object'
1031

1132
export const assetFromAssetSelectedMessage = (
1233
message: AssetSelectedMessage,
1334
): Asset => {
14-
const {
15-
uid: _uid,
16-
action: _action,
17-
field: _field,
18-
callbackId: _callbackId,
19-
...asset
20-
} = message
35+
const { asset } = message
2136
return asset
2237
}

packages/field-plugin/src/messaging/pluginMessage/containerToPluginMessage/AssetSelectedMessage.test.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import {
33
isAssetSelectedMessage,
44
} from './AssetSelectedMessage'
55
import { isAsset } from './Asset'
6+
import { emptyAsset } from './Asset.test'
67

78
const stub: AssetSelectedMessage = {
89
action: 'asset-selected',
910
uid: '-preview',
1011
field: 'dummy-field',
1112
callbackId: 'test-callback-id',
1213
filename: 'https://somthing.com/myimage.jpg',
14+
asset: emptyAsset,
1315
}
1416

1517
describe('AssetSelectedMessage', function () {
@@ -19,14 +21,6 @@ describe('AssetSelectedMessage', function () {
1921
it('is an Asset', () => {
2022
expect(isAsset(stub)).toEqual(true)
2123
})
22-
it('allows unknown properties', () => {
23-
expect(
24-
isAssetSelectedMessage({
25-
...stub,
26-
anUnknownProperty: 'something',
27-
}),
28-
).toEqual(true)
29-
})
3024
describe('the action property', () => {
3125
it('equals "asset-selected"', () => {
3226
expect(
@@ -45,13 +39,8 @@ describe('AssetSelectedMessage', function () {
4539
})
4640
describe('the field property', () => {
4741
it('is optional', () => {
48-
expect(
49-
isAssetSelectedMessage({
50-
action: 'asset-selected',
51-
uid: '-preview',
52-
filename: 'https://somthing.com/myimage.jpg',
53-
}),
54-
).toEqual(true)
42+
const { field: _field, ...withoutField } = stub
43+
expect(isAssetSelectedMessage(withoutField)).toEqual(true)
5544
})
5645
it('can be undefined', () => {
5746
expect(

packages/field-plugin/src/messaging/pluginMessage/containerToPluginMessage/AssetSelectedMessage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { isMessageToPlugin, MessageToPlugin } from './MessageToPlugin'
22
import { hasKey } from '../../../utils'
3-
import { Asset, isAsset } from './Asset'
3+
import { AssetWrapper, isAsset } from './Asset'
44

55
export type AssetSelectedMessage = MessageToPlugin<'asset-selected'> & {
66
field?: string
77
callbackId: string
8-
} & Asset &
8+
} & AssetWrapper &
99
Record<string, unknown>
1010

1111
export const isAssetSelectedMessage = (

packages/sandbox/src/components/FieldPluginSandbox.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ const useSandbox = (
252252
action: 'asset-selected',
253253
callbackId: message.callbackId,
254254
filename: `${originFromPluginParams(pluginParams)}/icon.svg`,
255+
asset: {
256+
id: 0,
257+
filename: `${originFromPluginParams(pluginParams)}/icon.svg`,
258+
fieldtype: 'asset',
259+
name: '',
260+
meta_data: {},
261+
title: '',
262+
copyright: '',
263+
focus: '',
264+
alt: '',
265+
source: '',
266+
is_private: false,
267+
},
255268
})
256269
},
257270
[uid, pluginParams, dispatchAssetSelected],

0 commit comments

Comments
 (0)