2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-19 15:51:01 -04:00
|
|
|
import {CommonModule, JsonPipe} from '@angular/common';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {Component} from '@angular/core';
|
2016-08-15 16:52:57 -04:00
|
|
|
import {TestBed, async} from '@angular/core/testing';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2015-05-26 20:12:38 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('JsonPipe', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const regNewLine = '\n';
|
|
|
|
let inceptionObj: any;
|
|
|
|
let inceptionObjString: string;
|
|
|
|
let pipe: JsonPipe;
|
2015-05-26 20:12:38 -04:00
|
|
|
|
2016-10-06 18:10:27 -04:00
|
|
|
function normalize(obj: string): string { return obj.replace(regNewLine, ''); }
|
2015-05-26 20:12:38 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2015-06-12 10:50:45 -04:00
|
|
|
inceptionObj = {dream: {dream: {dream: 'Limbo'}}};
|
2016-06-08 19:38:52 -04:00
|
|
|
inceptionObjString = '{\n' +
|
|
|
|
' "dream": {\n' +
|
|
|
|
' "dream": {\n' +
|
|
|
|
' "dream": "Limbo"\n' +
|
|
|
|
' }\n' +
|
|
|
|
' }\n' +
|
|
|
|
'}';
|
2015-05-26 20:12:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
pipe = new JsonPipe();
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('transform', () => {
|
|
|
|
it('should return JSON-formatted string',
|
2015-05-26 20:12:38 -04:00
|
|
|
() => { expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString); });
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should return JSON-formatted string even when normalized', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const dream1 = normalize(pipe.transform(inceptionObj));
|
|
|
|
const dream2 = normalize(inceptionObjString);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(dream1).toEqual(dream2);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should return JSON-formatted string similar to Json.stringify', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const dream1 = normalize(pipe.transform(inceptionObj));
|
|
|
|
const dream2 = normalize(JSON.stringify(inceptionObj, null, 2));
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(dream1).toEqual(dream2);
|
|
|
|
});
|
2015-10-27 15:43:34 -04:00
|
|
|
});
|
2015-05-26 20:12:38 -04:00
|
|
|
|
2015-10-27 15:43:34 -04:00
|
|
|
describe('integration', () => {
|
2016-08-15 16:52:57 -04:00
|
|
|
|
2016-08-19 15:51:01 -04:00
|
|
|
@Component({selector: 'test-comp', template: '{{data | json}}'})
|
|
|
|
class TestComp {
|
|
|
|
data: any;
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:52:57 -04:00
|
|
|
beforeEach(() => {
|
2016-08-19 15:51:01 -04:00
|
|
|
TestBed.configureTestingModule({declarations: [TestComp], imports: [CommonModule]});
|
2016-08-15 16:52:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with mutable objects', async(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const fixture = TestBed.createComponent(TestComp);
|
|
|
|
const mutable: number[] = [1];
|
2016-09-09 15:04:38 -04:00
|
|
|
fixture.componentInstance.data = mutable;
|
2016-08-15 16:52:57 -04:00
|
|
|
fixture.detectChanges();
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('[\n 1\n]');
|
2016-08-15 16:52:57 -04:00
|
|
|
|
|
|
|
mutable.push(2);
|
|
|
|
fixture.detectChanges();
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('[\n 1,\n 2\n]');
|
2016-08-15 16:52:57 -04:00
|
|
|
}));
|
2015-05-26 20:12:38 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|