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

Fix isEmpty logic in keepLatest #608

Merged
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
4 changes: 2 additions & 2 deletions ember-resources/src/util/keep-latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const isEmpty = (x: undefined | unknown | unknown[]) => {
if (typeof x === 'object') {
if (x === null) return true;

return x || Object.keys(x).length > 0;
return Object.keys(x).length === 0;
}

return Boolean(x);
return x !== 0 && !x;
};

interface Options<T = unknown> {
Expand Down
143 changes: 142 additions & 1 deletion testing/ember-app/tests/utils/keep-latest/js-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { keepLatest } from 'ember-resources/util/keep-latest';
module('Utils | keepLatest | js', function (hooks) {
setupTest(hooks);

test('it works', async function (assert) {
test('it works with a trackedFunction', async function (assert) {
class Test {
@tracked x = 1;

Expand Down Expand Up @@ -45,4 +45,145 @@ module('Utils | keepLatest | js', function (hooks) {
await timeout(40);
assert.strictEqual(instance.data, 2);
});

test('it works if the value gets reset to undefined while loading', async function (assert) {
class Test {
@tracked isLoading = false;
@tracked value?: number = 3;

@use data = keepLatest({
when: () => this.isLoading,
value: () => this.value,
});
}

let instance = new Test();

assert.strictEqual(instance.data, 3);

instance.isLoading = true;
instance.value = undefined;

assert.strictEqual(instance.data, 3);
});

test('it works with array values correctly', async function (assert) {
class Test {
@tracked isLoading = false;
@tracked value = ['a'];

@use data = keepLatest({
when: () => this.isLoading,
value: () => this.value,
});
}

let instance = new Test();

assert.deepEqual(instance.data, ['a']);

instance.isLoading = true;

instance.value = ['b'];

assert.deepEqual(instance.data, ['b'], 'still returns the current value if it is not empty');

instance.value = [];

assert.deepEqual(
instance.data,
['b'],
'returns the previous value if the current value is empty'
);
});

test('it works with object values correctly', async function (assert) {
class Test {
@tracked isLoading = false;
@tracked value: Record<string, unknown> = { x: 3 };

@use data = keepLatest({
when: () => this.isLoading,
value: () => this.value,
});
}

let instance = new Test();

assert.deepEqual(instance.data, { x: 3 });

instance.isLoading = true;

instance.value = { y: 4 };

assert.deepEqual(instance.data, { y: 4 }, 'still returns the current value if it is not empty');

instance.value = {};

assert.deepEqual(
instance.data,
{ y: 4 },
'returns the previous value if the current value is empty'
);
});

test('it works with string values correctly', async function (assert) {
class Test {
@tracked isLoading = false;
@tracked value = 'one';

@use data = keepLatest({
when: () => this.isLoading,
value: () => this.value,
});
}

let instance = new Test();

assert.deepEqual(instance.data, 'one');

instance.isLoading = true;

instance.value = 'two';

assert.deepEqual(instance.data, 'two', 'still returns the current value if it is not empty');

instance.value = '';

assert.deepEqual(
instance.data,
'two',
'returns the previous value if the current value is empty'
);
});

test('it works with number values correctly', async function (assert) {
class Test {
@tracked isLoading = false;
@tracked value: number | null = 1;

@use data = keepLatest({
when: () => this.isLoading,
value: () => this.value,
});
}

let instance = new Test();

assert.deepEqual(instance.data, 1);

instance.isLoading = true;

instance.value = 2;

assert.deepEqual(instance.data, 2, 'still returns the current value if it is not empty');

instance.value = 0;

assert.deepEqual(instance.data, 0, 'does not treat 0 as empty');

instance.value = null;

assert.deepEqual(instance.data, 0, 'returns the previous value if the current value is null');
});
});