-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(common): add test helper #307
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
180 changes: 152 additions & 28 deletions
180
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,167 @@ | ||
import { | ||
isAssetModalChangeMessage, | ||
isGetContextMessage, | ||
isHeightChangeMessage, | ||
isModalChangeMessage, | ||
isPluginLoadedMessage, | ||
isValueChangeMessage, | ||
} from '@storyblok/field-plugin' | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import { describe, test, expect, vi } 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: '', | ||
|
||
const fakeContainer = (sendToFieldPlugin) => { | ||
const schema = { | ||
field_type: 'test-field-plugin', | ||
options: [], | ||
} | ||
let height = undefined | ||
let uid = 'test-uid' | ||
let model = 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' | ||
|
||
const stateMessage = ({ action, callbackId }) => ({ | ||
callbackId: callbackId, | ||
schema, | ||
model, | ||
isModalOpen, | ||
uid, | ||
blockId, | ||
story: { | ||
content: {}, | ||
}, | ||
storyId: undefined, | ||
blockUid: undefined, | ||
token: undefined, | ||
uid: '', | ||
}, | ||
actions: { | ||
setContent: vi.fn(), | ||
setModalOpen: vi.fn(), | ||
requestContext: vi.fn(), | ||
selectAsset: vi.fn(), | ||
}, | ||
language, | ||
storyId, | ||
spaceId, | ||
token, | ||
action, | ||
}) | ||
|
||
return { | ||
receive: ({ data, origin }) => { | ||
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 | ||
sendToFieldPlugin( | ||
stateMessage({ | ||
action: 'state-changed', | ||
callbackId: data.callbackId, | ||
}), | ||
) | ||
} else if (isAssetModalChangeMessage(data)) { | ||
sendToFieldPlugin({ | ||
action: 'asset-selected', | ||
uid, | ||
filename: 'https://...', | ||
field: '', | ||
callbackId: data.callbackId, | ||
}) | ||
} else if (isGetContextMessage(data)) { | ||
sendToFieldPlugin({ | ||
action: 'get-context', | ||
uid, | ||
callbackId: data.callbackId, | ||
story: { | ||
content: {}, | ||
}, | ||
}) | ||
} else { | ||
console.warn( | ||
`Container received unknown message from plugin: ${JSON.stringify( | ||
data, | ||
)}`, | ||
) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
const setup = () => { | ||
let handleEvent | ||
const listener = (data) => { | ||
handleEvent({ data }) | ||
} | ||
const container = fakeContainer(listener) | ||
global.ResizeObserver = class ResizeObserver { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could extract the observer mocking in a separate method, to shorten the setup function a bit. |
||
observe() { | ||
// do nothing | ||
} | ||
unobserve() { | ||
// do nothing | ||
} | ||
disconnect() { | ||
// do nothing | ||
} | ||
} | ||
vi.stubGlobal('parent', { | ||
...global.parent, | ||
postMessage: vi.mocked((data: unknown, 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) | ||
} | ||
}) | ||
} | ||
|
||
describe('FieldPluginExammple', () => { | ||
test('should work', () => { | ||
vi.mocked(useFieldPlugin<number | undefined>).mockReturnValue( | ||
fieldPluginDefault, | ||
) | ||
afterEach(() => { | ||
vi.unstubAllGlobals() | ||
}) | ||
|
||
test('should render the component', () => { | ||
setup() | ||
BibiSebi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
render(<FieldPlugin />) | ||
const headline = screen.getByText('Field Value') | ||
expect(headline).toBeInTheDocument() | ||
}) | ||
|
||
test('should increase the counter', async () => { | ||
setup() | ||
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') | ||
}) | ||
}) |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
instead of if --> if else --> if else ..... I think early exit of the function could make this logic a bit more readable.