Skip to content
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

Try to reproduce an issue #992

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions test-app/tests/utils/function/js-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

import { resource, resourceFactory, use } from 'ember-resources';
import { trackedFunction } from 'ember-resources/util/function';

module('Utils | trackedFunction | js', function (hooks) {
Expand Down Expand Up @@ -117,4 +118,60 @@ module('Utils | trackedFunction | js', function (hooks) {

assert.strictEqual(foo.data.value, 12);
});

test('value will be accessible and resolved when isLoading becomes false', async function (assert) {
class Test {
@tracked count = 1;

data = trackedFunction(this, async () => {
let count = this.count;

// Pretend we're doing async work
await Promise.resolve();

return count * 2;
});
}

let foo = new Test();

assert.true(foo.data.isLoading);

await settled();

assert.false(foo.data.isLoading);
assert.strictEqual(foo.data.value, 2);
});

test('used resources can receive the the same state', async function (assert) {
const Custom = resourceFactory(() => {
return resource(({ use }) => {
let reactive = use(
trackedFunction(async () => {
// Pretend we're doing async work
await Promise.resolve();

return 2;
})
);

return () => reactive.current;
});
});

class Test {
@tracked count = 1;

@use data = Custom();
}

let foo = new Test();

assert.true(foo.data.isLoading);

await settled();

assert.false(foo.data.isLoading);
assert.strictEqual(foo.data.value, 2);
});
});
Loading