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-02 18:53:34 -04:00
|
|
|
import {JsonPipe} from '@angular/common';
|
|
|
|
import {Component} from '@angular/core';
|
2016-07-11 19:04:32 -04:00
|
|
|
import {TestComponentBuilder} from '@angular/core/testing';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {AsyncTestCompleter, afterEach, beforeEach, ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
|
|
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
2015-05-26 20:12:38 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
import {Json, StringWrapper} from '../../src/facade/lang';
|
2015-05-26 20:12:38 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('JsonPipe', () => {
|
2015-05-26 20:12:38 -04:00
|
|
|
var regNewLine = '\n';
|
2016-06-17 13:57:32 -04:00
|
|
|
var inceptionObj: any;
|
|
|
|
var inceptionObjString: string;
|
|
|
|
var pipe: JsonPipe;
|
2015-05-26 20:12:38 -04:00
|
|
|
|
|
|
|
function normalize(obj: string): string { return StringWrapper.replace(obj, regNewLine, ''); }
|
|
|
|
|
|
|
|
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', () => {
|
2015-05-26 20:12:38 -04:00
|
|
|
var dream1 = normalize(pipe.transform(inceptionObj));
|
|
|
|
var dream2 = normalize(inceptionObjString);
|
|
|
|
expect(dream1).toEqual(dream2);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should return JSON-formatted string similar to Json.stringify', () => {
|
2015-05-26 20:12:38 -04:00
|
|
|
var dream1 = normalize(pipe.transform(inceptionObj));
|
|
|
|
var dream2 = normalize(Json.stringify(inceptionObj));
|
|
|
|
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', () => {
|
|
|
|
it('should work with mutable objects',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[TestComponentBuilder, AsyncTestCompleter],
|
|
|
|
(tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
|
|
|
tcb.createAsync(TestComp).then((fixture) => {
|
|
|
|
let mutable: number[] = [1];
|
|
|
|
fixture.debugElement.componentInstance.data = mutable;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('[\n 1\n]');
|
2015-10-27 15:43:34 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
mutable.push(2);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('[\n 1,\n 2\n]');
|
2015-10-27 15:43:34 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 20:12:38 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-10-27 15:43:34 -04:00
|
|
|
|
|
|
|
@Component({selector: 'test-comp', template: '{{data | json}}', pipes: [JsonPipe]})
|
|
|
|
class TestComp {
|
|
|
|
data: any;
|
|
|
|
}
|