From 6435ecd3c6cde5ef9bfcae6c48f8620fdcb51723 Mon Sep 17 00:00:00 2001 From: adasilva Date: Mon, 12 Feb 2018 16:03:53 -0500 Subject: [PATCH] fix(platform-browser): support 0/false/null values in transfer_state (#22179) Issue #22178 PR Close #22179 --- .../src/browser/transfer_state.ts | 4 +++- .../test/browser/transfer_state_spec.ts | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/platform-browser/src/browser/transfer_state.ts b/packages/platform-browser/src/browser/transfer_state.ts index 5748b6b5ff..4a765961b8 100644 --- a/packages/platform-browser/src/browser/transfer_state.ts +++ b/packages/platform-browser/src/browser/transfer_state.ts @@ -93,7 +93,9 @@ export class TransferState { /** * Get the value corresponding to a key. Return `defaultValue` if key is not found. */ - get(key: StateKey, defaultValue: T): T { return this.store[key] as T || defaultValue; } + get(key: StateKey, defaultValue: T): T { + return this.store[key] !== undefined ? this.store[key] as T : defaultValue; + } /** * Set the value corresponding to a key. diff --git a/packages/platform-browser/test/browser/transfer_state_spec.ts b/packages/platform-browser/test/browser/transfer_state_spec.ts index 9235ffbb6c..d3418c518d 100644 --- a/packages/platform-browser/test/browser/transfer_state_spec.ts +++ b/packages/platform-browser/test/browser/transfer_state_spec.ts @@ -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);