-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add some timing tests for trackedFunction #1011
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e98c121
Add some timing tests for trackedFunction
NullVoxPopuli 5168dd4
Update tests
NullVoxPopuli 99e728e
Fix #1010
NullVoxPopuli 606ba4b
Add changeset
NullVoxPopuli 407ccf7
Add a comment and don't nullify if we don't need to
NullVoxPopuli 09bbad6
Whoops
NullVoxPopuli 8e79ff5
Don't read before set
NullVoxPopuli c982307
trigger ci
NullVoxPopuli 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"ember-resources": patch | ||
--- | ||
|
||
`trackedFunction`: Fix timing issue where updating tracked data consumed in `trackedFunction` would not re-cause the `isLoading` state to become `true` again. | ||
|
||
Resolves #1010 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import Component from '@glimmer/component'; | ||
import { concat } from '@ember/helper'; | ||
import { render, settled } from '@ember/test-helpers'; | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
|
||
import { cell, resource, resourceFactory } from 'ember-resources'; | ||
import { trackedFunction } from 'ember-resources/util/function'; | ||
|
||
module('Utils | trackedFunction | timing', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('With Argument', async function (assert) { | ||
let step = (msg: string) => assert.step(msg); | ||
|
||
let state = cell(0); | ||
|
||
async function fn(value: number) { | ||
step(`fn:begin:${value}`); | ||
await Promise.resolve(); | ||
step(`fn:end:${value}`); | ||
return `yay:${value}`; | ||
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. 😄 |
||
} | ||
|
||
const WithArgument = resourceFactory(num => resource(({ use }) => { | ||
let reactive = use(trackedFunction(() => fn(num))); | ||
|
||
// TODO: the types should allow us to directly return the use, | ||
// but they don't currently | ||
return () => reactive.current; | ||
})); | ||
|
||
await render( | ||
<template> | ||
{{#let (WithArgument state.current) as |request|}} | ||
{{#if request.isLoading}} | ||
{{step 'loading'}} | ||
{{/if}} | ||
{{#if request.isError}} | ||
{{step 'error'}} | ||
{{/if}} | ||
{{#if request.value}} | ||
{{step (concat 'loaded:' request.value)}} | ||
{{/if}} | ||
{{/let}} | ||
</template> | ||
); | ||
|
||
assert.verifySteps([ | ||
'fn:begin:0', 'loading', 'fn:end:0', 'loaded:yay:0' | ||
]); | ||
|
||
state.current = 1; | ||
await settled(); | ||
|
||
assert.verifySteps([ | ||
'fn:begin:1', 'loading', 'fn:end:1', 'loaded:yay:1' | ||
]); | ||
}); | ||
|
||
test('From a component class', async function (assert) { | ||
let step = (msg: string) => assert.step(msg); | ||
|
||
let state = cell(0); | ||
|
||
class Example extends Component<{ Args: { value: unknown } }> { | ||
request = trackedFunction(this, async () => { | ||
let value = this.args.value; | ||
step(`fn:begin:${value}`); | ||
await Promise.resolve(); | ||
step(`fn:end:${value}`); | ||
return `yay:${value}`; | ||
}); | ||
|
||
<template> | ||
{{#if this.request.isPending}} | ||
{{step 'pending'}} | ||
{{/if}} | ||
{{#if this.request.isLoading}} | ||
{{step 'loading'}} | ||
{{/if}} | ||
{{#if this.request.isError}} | ||
{{step 'error'}} | ||
{{/if}} | ||
{{#if this.request.value}} | ||
{{step (concat 'loaded:' this.request.value)}} | ||
{{/if}} | ||
{{#if this.request.isFinished}} | ||
{{step 'finished'}} | ||
{{/if}} | ||
</template> | ||
} | ||
|
||
await render( | ||
<template> | ||
<Example @value={{state.current}} /> | ||
</template> | ||
); | ||
|
||
assert.verifySteps([ | ||
'fn:begin:0', 'pending', 'loading', 'fn:end:0', 'loaded:yay:0', 'finished' | ||
]); | ||
|
||
state.current = 1; | ||
await settled(); | ||
|
||
assert.verifySteps([ | ||
'fn:begin:1', 'pending', 'loading', 'fn:end:1', 'loaded:yay:1', 'finished' | ||
]); | ||
}); | ||
}); |
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.
Looks like this cell import is unused, probably leftover from investigation
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.
thanks, yeah, I need to fix linting on this repo 🙃
there is a bug with prettier rn though, and I'm waiting for them to do another release