-
Notifications
You must be signed in to change notification settings - Fork 278
Add tests using async components #105
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,114 @@ | ||
| import { defineAsyncComponent, defineComponent, h, AppConfig } from 'vue' | ||
| import flushPromises from 'flush-promises' | ||
|
|
||
| import { mount } from '../../src' | ||
|
|
||
| const config: AppConfig = { | ||
| devtools: false, | ||
| optionMergeStrategies: {}, | ||
| globalProperties: {}, | ||
| isCustomElement: (tag: string) => false, | ||
| performance: false, | ||
| errorHandler: (error: Error) => { | ||
| if (error.message.match(/Async component failed to load./)) { | ||
| return | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| // AsyncComponents are documented here: https://github.com/vuejs/rfcs/blob/async-component/active-rfcs/0026-async-component-api.md | ||
| describe('defineAsyncComponent', () => { | ||
| beforeAll(jest.useFakeTimers) | ||
| afterAll(jest.useRealTimers) | ||
|
|
||
| it('works with the basic usage', async () => { | ||
| const AsyncHello = defineAsyncComponent(() => | ||
| import('../components/Hello.vue') | ||
| ) | ||
| const Comp = defineComponent({ | ||
| render() { | ||
| return h('div', [h(AsyncHello)]) | ||
| } | ||
| }) | ||
|
|
||
| const wrapper = mount(Comp, { global: { config } }) | ||
| await flushPromises() | ||
| expect(wrapper.html()).toContain('Hello world') | ||
| }) | ||
|
|
||
| it('works with options usage', async () => { | ||
| const Async = defineAsyncComponent({ | ||
| loader: () => | ||
| new Promise<any>((res) => { | ||
| setTimeout(() => { | ||
| res({ | ||
| template: '<div>Async Component</div>' | ||
| }) | ||
| }, 75) | ||
| }), | ||
| loadingComponent: { | ||
| template: '<div>Loading Component</div>' | ||
| }, | ||
| delay: 10 | ||
| }) | ||
|
|
||
| const Comp = defineComponent({ | ||
| render() { | ||
| return h('div', [h(Async)]) | ||
| } | ||
| }) | ||
|
|
||
| const wrapper = mount(Comp, { global: { config } }) | ||
| jest.runTimersToTime(35) | ||
| await flushPromises() | ||
| expect(wrapper.html()).toContain('Loading Component') | ||
|
|
||
| jest.runTimersToTime(100) | ||
| await flushPromises() | ||
| expect(wrapper.html()).toContain('Async Component') | ||
| }) | ||
|
|
||
| it('catches error and renders ErrorComponent', async () => { | ||
| const Async = defineAsyncComponent({ | ||
| loader: () => | ||
| new Promise<any>((res, rej) => { | ||
| rej('Async component failed to load.') | ||
| }), | ||
| errorComponent: { | ||
| template: '<div>Error Component</div>' | ||
| }, | ||
| onError(error, retry, fail, attempts) { | ||
| fail() | ||
| } | ||
| }) | ||
|
|
||
| const Comp = defineComponent({ | ||
| render() { | ||
| return h('div', [h(Async)]) | ||
| } | ||
| }) | ||
|
|
||
| const wrapper = mount(Comp, { global: { config } }) | ||
| await flushPromises() | ||
|
|
||
| expect(wrapper.html()).toContain('Error Component') | ||
| }) | ||
|
|
||
| // TODO: Find out why this does not work | ||
| // Is it valid to have an AsyncComponent as the root? Was it ever? | ||
| it.skip('works when AsyncComponent is the root', async () => { | ||
| const AsyncHello = defineAsyncComponent(() => | ||
| import('../components/Hello.vue') | ||
| ) | ||
| const Comp = defineComponent({ | ||
| render() { | ||
| return h(AsyncHello) | ||
| } | ||
| }) | ||
|
|
||
| const wrapper = mount(Comp, { global: { config } }) | ||
| await flushPromises() | ||
| expect(wrapper.html()).toContain('Hello world') | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think this
onErroris a bit redundant? The option is used to have some retry control, but here we're just failing no matter what.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.
Hm, also the test is passing but it yields some warns and errors:
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.
Not sure if these error logs are somewhat expected, and we should just mock console for the test and call it a day… 🤔
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.
I had this thought too. Some other tests also throw and error. Is there any benefit to mocking the console? If printing the error is correct, I don't keep it.
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.
I tend to mute them to make it the output cleaner in the long run, but I don't care much either. Unless the console error is part of the expected output, then I spy it and assert the times called and so.
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.
(I still believe the
onErrorbit can be removed)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.
We can have our own Vue
errorHandler, to easily assert on it without mockingconsole.errorThere 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.
I will make this change, thanks