Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion reactiveweb/src/keep-latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function keepLatest<Return = unknown>({ when, value: valueFn }: Options<R
if (previous === undefined && initial) {
initial = false;

return value;
return (previous = value);
}

return (previous = isEmpty(value) ? previous : value);
Expand Down
20 changes: 20 additions & 0 deletions tests/test-app/tests/utils/keep-latest/js-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions tests/test-app/tests/utils/keep-latest/rendering-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<template>{{instance.data}}</template>);

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 = [];
Expand Down
20 changes: 20 additions & 0 deletions tests/vite-app/tests/utils/keep-latest/js-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions tests/vite-app/tests/utils/keep-latest/rendering-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<template>{{instance.data}}</template>);

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 = [];
Expand Down
Loading