-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
stringify.spec.js
76 lines (57 loc) · 2.22 KB
/
stringify.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* global __karma__ */
var chai = require('chai')
var expect = chai.expect
var stringify = require('../../client/stringify')
describe('stringify', function () {
it('should serialize string', function () {
expect(stringify('aaa')).to.be.eql("'aaa'")
})
it('should serialize booleans', function () {
expect(stringify(true)).to.be.eql('true')
expect(stringify(false)).to.be.eql('false')
})
it('should serialize null and undefined', function () {
expect(stringify(null)).to.be.eql('null')
expect(stringify()).to.be.eql('undefined')
})
it('should serialize functions', function () {
function abc (a, b, c) { return 'whatever' }
var def = function (d, e, f) { return 'whatever' }
expect(stringify(abc)).to.be.eql('function abc(a, b, c) { ... }')
expect(stringify(def)).to.be.eql('function (d, e, f) { ... }')
})
it('should serialize arrays', function () {
expect(stringify(['a', 'b', null, true, false])).to.be.eql("['a', 'b', null, true, false]")
})
it('should serialize objects', function () {
var obj
obj = {a: 'a', b: 'b', c: null, d: true, e: false}
expect(stringify(obj)).to.be.eql("Object{a: 'a', b: 'b', c: null, d: true, e: false}")
function MyObj () {
this.a = 'a'
}
obj = new MyObj()
expect(stringify(obj)).to.be.eql("MyObj{a: 'a'}")
obj = {constructor: null}
expect(stringify(obj)).to.be.eql('Object{constructor: null}')
obj = Object.create(null)
obj.a = 'a'
expect(stringify(obj)).to.be.eql("Object{a: 'a'}")
})
it('should serialize html', function () {
var div = document.createElement('div')
expect(stringify(div)).to.be.eql('<div></div>')
div.innerHTML = 'some <span>text</span>'
expect(stringify(div)).to.be.eql('<div>some <span>text</span></div>')
})
it('should serialize DOMParser objects', function () {
var parser = new DOMParser()
var doc = parser.parseFromString('<test></test>', 'application/xml')
expect(stringify(doc)).to.be.eql('<test></test>')
})
it('should serialize across iframes', function () {
var div = document.createElement('div')
expect(__karma__.stringify(div)).to.be.eql('<div></div>')
expect(__karma__.stringify([1, 2])).to.be.eql('[1, 2]')
})
})