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(platform-browser): support 0/false/null values in transfer_state #22179

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/platform-browser/src/browser/transfer_state.ts
Expand Up @@ -93,7 +93,9 @@ export class TransferState {
/**
* Get the value corresponding to a key. Return `defaultValue` if key is not found.
*/
get<T>(key: StateKey<T>, defaultValue: T): T { return this.store[key] as T || defaultValue; }
get<T>(key: StateKey<T>, defaultValue: T): T {
return this.store[key] !== undefined ? this.store[key] as T : defaultValue;
}

/**
* Set the value corresponding to a key.
Expand Down
21 changes: 21 additions & 0 deletions packages/platform-browser/test/browser/transfer_state_spec.ts
Expand Up @@ -70,6 +70,27 @@ import {DOCUMENT} from '@angular/platform-browser/src/dom/dom_tokens';
expect(transferState.hasKey(TEST_KEY)).toBe(true);
});

it('supports setting and accessing value \'0\' via get', () => {
const transferState: TransferState = TestBed.get(TransferState);
transferState.set(TEST_KEY, 0);
expect(transferState.get(TEST_KEY, 20)).toBe(0);
expect(transferState.hasKey(TEST_KEY)).toBe(true);
});

it('supports setting and accessing value \'false\' via get', () => {
const transferState: TransferState = TestBed.get(TransferState);
transferState.set(TEST_KEY, false);
expect(transferState.get(TEST_KEY, 20)).toBe(false);
expect(transferState.hasKey(TEST_KEY)).toBe(true);
});

it('supports setting and accessing value \'null\' via get', () => {
const transferState: TransferState = TestBed.get(TransferState);
transferState.set(TEST_KEY, null);
expect(transferState.get(TEST_KEY, 20)).toBe(null);
expect(transferState.hasKey(TEST_KEY)).toBe(true);
});

it('supports removing keys', () => {
const transferState: TransferState = TestBed.get(TransferState);
transferState.set(TEST_KEY, 20);
Expand Down