From b5314e96425a8792407529a328752622391c2bda Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:20:41 -0400 Subject: [PATCH 1/2] Does it work --- test-app/tests/utils/function/js-test.ts | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test-app/tests/utils/function/js-test.ts b/test-app/tests/utils/function/js-test.ts index 78dfefb8e..2d7b6a9de 100644 --- a/test-app/tests/utils/function/js-test.ts +++ b/test-app/tests/utils/function/js-test.ts @@ -117,4 +117,29 @@ 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); + + }); }); From a54e72e90c247833272db1e3348f0d2f9559accc Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:35:18 -0400 Subject: [PATCH 2/2] Another test --- test-app/tests/utils/function/js-test.ts | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test-app/tests/utils/function/js-test.ts b/test-app/tests/utils/function/js-test.ts index 2d7b6a9de..ea95ee941 100644 --- a/test-app/tests/utils/function/js-test.ts +++ b/test-app/tests/utils/function/js-test.ts @@ -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) { @@ -140,6 +141,37 @@ module('Utils | trackedFunction | js', function (hooks) { 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); }); });