diff --git a/reactiveweb/src/keep-latest.ts b/reactiveweb/src/keep-latest.ts index b2fb052e..b2f97ec0 100644 --- a/reactiveweb/src/keep-latest.ts +++ b/reactiveweb/src/keep-latest.ts @@ -107,7 +107,7 @@ export function keepLatest({ when, value: valueFn }: Options this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + assert.strictEqual(instance.data, 3); + + instance.value = undefined; + + assert.strictEqual(instance.data, 3, 'returns the initial value while the value is empty'); + }); + test('it works with array values correctly', async function (assert) { class Test { @tracked isLoading = false; diff --git a/tests/test-app/tests/utils/keep-latest/rendering-test.gts b/tests/test-app/tests/utils/keep-latest/rendering-test.gts index f5695f29..68ad97a1 100644 --- a/tests/test-app/tests/utils/keep-latest/rendering-test.gts +++ b/tests/test-app/tests/utils/keep-latest/rendering-test.gts @@ -59,6 +59,29 @@ module('Utils | keepLatest | rendering', function (hooks) { assert.dom().hasText('2'); }); + test('it keeps the initial value when it starts out loading', async function (assert) { + class Test { + @tracked isLoading = true; + @tracked value?: number = 3; + + @use data = keepLatest({ + when: () => this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + await render(); + + assert.dom().hasText('3'); + + instance.value = undefined; + await settled(); + + assert.dom().hasText('3', 'renders the initial value while the value is empty'); + }); + test('if the previous value is not empty, and the current value is empty', async function (assert) { class Test { @tracked x: number[] | number = []; diff --git a/tests/vite-app/tests/utils/keep-latest/js-test.ts b/tests/vite-app/tests/utils/keep-latest/js-test.ts index 2bc2c9a5..5244725c 100644 --- a/tests/vite-app/tests/utils/keep-latest/js-test.ts +++ b/tests/vite-app/tests/utils/keep-latest/js-test.ts @@ -68,6 +68,26 @@ module('Utils | keepLatest | js', function (hooks) { assert.strictEqual(instance.data, 3); }); + test('it keeps the initial value when it starts out loading', async function (assert) { + class Test { + @tracked isLoading = true; + @tracked value?: number = 3; + + @use data = keepLatest({ + when: () => this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + assert.strictEqual(instance.data, 3); + + instance.value = undefined; + + assert.strictEqual(instance.data, 3, 'returns the initial value while the value is empty'); + }); + test('it works with array values correctly', async function (assert) { class Test { @tracked isLoading = false; diff --git a/tests/vite-app/tests/utils/keep-latest/rendering-test.gts b/tests/vite-app/tests/utils/keep-latest/rendering-test.gts index f5695f29..68ad97a1 100644 --- a/tests/vite-app/tests/utils/keep-latest/rendering-test.gts +++ b/tests/vite-app/tests/utils/keep-latest/rendering-test.gts @@ -59,6 +59,29 @@ module('Utils | keepLatest | rendering', function (hooks) { assert.dom().hasText('2'); }); + test('it keeps the initial value when it starts out loading', async function (assert) { + class Test { + @tracked isLoading = true; + @tracked value?: number = 3; + + @use data = keepLatest({ + when: () => this.isLoading, + value: () => this.value, + }); + } + + const instance = new Test(); + + await render(); + + assert.dom().hasText('3'); + + instance.value = undefined; + await settled(); + + assert.dom().hasText('3', 'renders the initial value while the value is empty'); + }); + test('if the previous value is not empty, and the current value is empty', async function (assert) { class Test { @tracked x: number[] | number = [];