fix(platform-browser): support 0/false/null values in transfer_state (#22179)

Issue #22178

PR Close #22179
This commit is contained in:
adasilva 2018-02-12 16:03:53 -05:00 committed by Miško Hevery
parent 16d1700a8e
commit 6435ecd3c6
2 changed files with 24 additions and 1 deletions

View File

@ -93,7 +93,9 @@ export class TransferState {
/** /**
* Get the value corresponding to a key. Return `defaultValue` if key is not found. * 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. * Set the value corresponding to a key.

View File

@ -70,6 +70,27 @@ import {DOCUMENT} from '@angular/platform-browser/src/dom/dom_tokens';
expect(transferState.hasKey(TEST_KEY)).toBe(true); 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', () => { it('supports removing keys', () => {
const transferState: TransferState = TestBed.get(TransferState); const transferState: TransferState = TestBed.get(TransferState);
transferState.set(TEST_KEY, 20); transferState.set(TEST_KEY, 20);