-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add test helper (#307)
* WIP * chore(lib): release @storyblok/field-plugin@1.0.0 (#305) * fix test and clean up * renames * fix(common): fix type errors in test helper (#320) * clean up test helper WIP * improve types * update snapshot
- Loading branch information
1 parent
2d5b413
commit 7d2f396
Showing
10 changed files
with
1,374 additions
and
557 deletions.
There are no files selected for viewing
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
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
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
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
54 changes: 19 additions & 35 deletions
54
packages/cli/templates/react/src/components/FieldPluginExample/index.spec.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,43 +1,27 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { describe, test, expect, vi } from 'vitest' | ||
|
||
import userEvent from '@testing-library/user-event' | ||
import { describe, test, expect } from 'vitest' | ||
import FieldPlugin from '.' | ||
import { useFieldPlugin } from '@storyblok/field-plugin/react' | ||
import { FieldPluginResponse } from '@storyblok/field-plugin' | ||
|
||
vi.mock('@storyblok/field-plugin/react') | ||
|
||
const fieldPluginDefault: FieldPluginResponse<number | undefined> = { | ||
type: 'loaded', | ||
data: { | ||
isModalOpen: false, | ||
content: undefined, | ||
options: {}, | ||
spaceId: undefined, | ||
storyLang: '', | ||
story: { | ||
content: {}, | ||
}, | ||
storyId: undefined, | ||
blockUid: undefined, | ||
token: undefined, | ||
uid: '', | ||
}, | ||
actions: { | ||
setContent: vi.fn(), | ||
setModalOpen: vi.fn(), | ||
requestContext: vi.fn(), | ||
selectAsset: vi.fn(), | ||
}, | ||
} | ||
import { setupFieldPlugin } from '../../../test' | ||
|
||
describe('FieldPluginExammple', () => { | ||
test('should work', () => { | ||
vi.mocked(useFieldPlugin<number | undefined>).mockReturnValue( | ||
fieldPluginDefault, | ||
) | ||
describe('FieldPluginExample', () => { | ||
test('should render the component', () => { | ||
const { cleanUp } = setupFieldPlugin() | ||
render(<FieldPlugin />) | ||
const headline = screen.getByText('Field Value') | ||
expect(headline).toBeInTheDocument() | ||
cleanUp() | ||
}) | ||
|
||
test('should increase the counter', async () => { | ||
const { cleanUp } = setupFieldPlugin() | ||
const user = userEvent.setup() | ||
render(<FieldPlugin />) | ||
expect(screen.getByTestId('count').textContent).toEqual('0') | ||
await user.click(screen.getByText('Increment')) | ||
expect(screen.getByTestId('count').textContent).toEqual('1') | ||
await user.click(screen.getByText('Increment')) | ||
expect(screen.getByTestId('count').textContent).toEqual('2') | ||
cleanUp() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { | ||
isAssetModalChangeMessage, | ||
isGetContextMessage, | ||
isHeightChangeMessage, | ||
isModalChangeMessage, | ||
isPluginLoadedMessage, | ||
isValueChangeMessage, | ||
} from '@storyblok/field-plugin' | ||
|
||
import { vi } from 'vitest' | ||
|
||
const getContainer = (sendToFieldPlugin: (data: unknown) => void) => { | ||
const schema = { | ||
field_type: 'test-field-plugin', | ||
options: [], | ||
} | ||
// @ts-ignore `height` is not used anywhere, but we keep it here. | ||
let height = undefined | ||
let uid = 'test-uid' | ||
let model: any = undefined | ||
let isModalOpen = false | ||
let blockId = 'test-block-id' | ||
let language = 'default' | ||
let storyId = 'test-story-id' | ||
let spaceId = 'test-space-id' | ||
let token = 'test-token' | ||
let story = { | ||
content: {}, | ||
} | ||
|
||
const stateMessage = ({ | ||
action, | ||
callbackId, | ||
}: { | ||
action: 'loaded' | 'state-changed' | ||
callbackId: string | ||
}) => ({ | ||
callbackId: callbackId, | ||
schema, | ||
model, | ||
isModalOpen, | ||
uid, | ||
blockId, | ||
story, | ||
language, | ||
storyId, | ||
spaceId, | ||
token, | ||
action, | ||
}) | ||
|
||
return { | ||
receive: ({ | ||
data, | ||
}: { | ||
data: { callbackId: string } & Record<string, any> | ||
origin: string | ||
}) => { | ||
if (isPluginLoadedMessage(data)) { | ||
sendToFieldPlugin( | ||
stateMessage({ | ||
action: 'loaded', | ||
callbackId: data.callbackId, | ||
}), | ||
) | ||
} else if (isValueChangeMessage(data)) { | ||
model = data.model | ||
sendToFieldPlugin( | ||
stateMessage({ | ||
action: 'state-changed', | ||
callbackId: data.callbackId, | ||
}), | ||
) | ||
} else if (isModalChangeMessage(data)) { | ||
isModalOpen = data.status | ||
sendToFieldPlugin( | ||
stateMessage({ | ||
action: 'state-changed', | ||
callbackId: data.callbackId, | ||
}), | ||
) | ||
} else if (isHeightChangeMessage(data)) { | ||
height = data.height | ||
} else if (isAssetModalChangeMessage(data)) { | ||
sendToFieldPlugin({ | ||
action: 'asset-selected', | ||
uid, | ||
filename: 'https://plugin-sandbox.storyblok.com/icon.svg', | ||
callbackId: data.callbackId, | ||
}) | ||
} else if (isGetContextMessage(data)) { | ||
sendToFieldPlugin({ | ||
action: 'get-context', | ||
uid, | ||
callbackId: data.callbackId, | ||
story, | ||
}) | ||
} else { | ||
console.warn( | ||
`Container received unknown message from plugin: ${JSON.stringify( | ||
data, | ||
)}`, | ||
) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export const setupFieldPlugin = () => { | ||
let handleEvent: (event: MessageEvent<unknown>) => void | ||
const container = getContainer((data: unknown) => { | ||
// @ts-ignore | ||
handleEvent({ data }) | ||
}) | ||
global.ResizeObserver = class ResizeObserver { | ||
observe() { | ||
// do nothing | ||
} | ||
unobserve() { | ||
// do nothing | ||
} | ||
disconnect() { | ||
// do nothing | ||
} | ||
} | ||
vi.stubGlobal('parent', { | ||
...global.parent, | ||
postMessage: vi.mocked( | ||
(data: { callbackId: string } & Record<string, any>, origin: string) => { | ||
container.receive({ data, origin }) | ||
}, | ||
), | ||
}) | ||
vi.stubGlobal('location', { | ||
...window.location, | ||
search: `?protocol=https%3A&host=plugin-sandbox.storyblok.com&uid=test-uid&preview=1`, | ||
}) | ||
|
||
const addEventListenerFallback = global.addEventListener | ||
vi.stubGlobal('addEventListener', (name: string, callback: EventListener) => { | ||
if (name === 'message') { | ||
handleEvent = callback | ||
} else { | ||
addEventListenerFallback.call(global, name, callback) | ||
} | ||
}) | ||
|
||
return { | ||
cleanUp: () => vi.unstubAllGlobals(), | ||
} | ||
} |
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
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
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
Oops, something went wrong.
7d2f396
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
plugin-sandbox – ./
storyblok-field-plugin-sandbox.vercel.app
plugin-sandbox.storyblok.com
plugin-sandbox-storyblok-com.vercel.app
plugin-sandbox-git-main-storyblok-com.vercel.app