@@ -7,7 +7,7 @@ describe("When working with PolyPlug,", function() {
7
7
plug = polyplug ( ) ;
8
8
} ) ;
9
9
10
- describe ( "when serializing to or from the DOM ," , function ( ) {
10
+ describe ( "when serializing to or from JS/HTML nodes ," , function ( ) {
11
11
12
12
beforeEach ( function ( ) {
13
13
// A fragment of HTML containing various generic elements we'll use to
@@ -30,150 +30,143 @@ describe("When working with PolyPlug,", function() {
30
30
<li>b</li>
31
31
<li>c</li>
32
32
</ul>` ;
33
- jsonString = `{
34
- "nodeType": 1,
35
- "tagName": "div",
36
- "attributes": {
37
- "id": "test-node",
38
- "style": "font-weight: bold;",
39
- "custom-attribute": ""
40
- },
41
- "childNodes": [
42
- {
33
+ jsRepresentation = {
43
34
"nodeType" : 1 ,
44
- "tagName": "p",
45
- "childNodes": [
46
- {
47
- "nodeType": 3,
48
- "nodeName": "#text",
49
- "nodeValue": "Textual content.",
50
- "childNodes": []
51
- }
52
- ]
53
- },
54
- {
55
- "nodeType": 8,
56
- "nodeName": "#comment",
57
- "nodeValue": " A comment ",
58
- "childNodes": []
59
- },
60
- {
61
- "nodeType": 1,
62
- "tagName": "form",
35
+ "tagName" : "div" ,
36
+ "attributes" : {
37
+ "id" : "test-node" ,
38
+ "style" : "font-weight: bold;" ,
39
+ "custom-attribute" : ""
40
+ } ,
63
41
"childNodes" : [
64
42
{
65
43
"nodeType" : 1 ,
66
- "tagName": "label",
67
- "attributes": {
68
- "for": "testInput"
69
- },
44
+ "tagName" : "p" ,
70
45
"childNodes" : [
71
46
{
72
47
"nodeType" : 3 ,
73
48
"nodeName" : "#text" ,
74
- "nodeValue": "Test Input ",
49
+ "nodeValue" : "Textual content. " ,
75
50
"childNodes" : [ ]
76
51
}
77
52
]
78
53
} ,
79
54
{
80
- "nodeType": 1,
81
- "tagName": "input",
82
- "attributes": {
83
- "type": "text",
84
- "name": "testInput",
85
- "value": "test"
86
- },
87
- "childNodes": []
88
- },
89
- {
90
- "nodeType": 1,
91
- "tagName": "textarea",
92
- "value": "Some free text content...",
55
+ "nodeType" : 8 ,
56
+ "nodeName" : "#comment" ,
57
+ "nodeValue" : " A comment " ,
93
58
"childNodes" : [ ]
94
59
} ,
95
60
{
96
61
"nodeType" : 1 ,
97
- "tagName": "input",
98
- "attributes": {
99
- "type": "submit",
100
- "value": "Submit"
101
- },
102
- "childNodes": []
103
- }
104
- ]
105
- },
106
- {
107
- "nodeType": 1,
108
- "tagName": "ul",
109
- "attributes": {
110
- "id": "list"
111
- },
112
- "childNodes": [
113
- {
114
- "nodeType": 1,
115
- "tagName": "li",
62
+ "tagName" : "form" ,
116
63
"childNodes" : [
117
64
{
118
- "nodeType": 3,
119
- "nodeName": "#text",
120
- "nodeValue": "a",
65
+ "nodeType" : 1 ,
66
+ "tagName" : "label" ,
67
+ "attributes" : {
68
+ "for" : "testInput"
69
+ } ,
70
+ "childNodes" : [
71
+ {
72
+ "nodeType" : 3 ,
73
+ "nodeName" : "#text" ,
74
+ "nodeValue" : "Test Input" ,
75
+ "childNodes" : [ ]
76
+ }
77
+ ]
78
+ } ,
79
+ {
80
+ "nodeType" : 1 ,
81
+ "tagName" : "input" ,
82
+ "attributes" : {
83
+ "type" : "text" ,
84
+ "name" : "testInput" ,
85
+ "value" : "test"
86
+ } ,
121
87
"childNodes" : [ ]
122
- }
123
- ]
124
- },
125
- {
126
- "nodeType": 1,
127
- "tagName": "li",
128
- "childNodes": [
88
+ } ,
129
89
{
130
- "nodeType": 3,
131
- "nodeName": "#text",
132
- "nodeValue": "b",
90
+ "nodeType" : 1 ,
91
+ "tagName" : "textarea" ,
92
+ "value" : "Some free text content..." ,
93
+ "childNodes" : [ ]
94
+ } ,
95
+ {
96
+ "nodeType" : 1 ,
97
+ "tagName" : "input" ,
98
+ "attributes" : {
99
+ "type" : "submit" ,
100
+ "value" : "Submit"
101
+ } ,
133
102
"childNodes" : [ ]
134
103
}
135
104
]
136
105
} ,
137
106
{
138
107
"nodeType" : 1 ,
139
- "tagName": "li",
108
+ "tagName" : "ul" ,
109
+ "attributes" : {
110
+ "id" : "list"
111
+ } ,
140
112
"childNodes" : [
141
113
{
142
- "nodeType": 3,
143
- "nodeName": "#text",
144
- "nodeValue": "c",
145
- "childNodes": []
114
+ "nodeType" : 1 ,
115
+ "tagName" : "li" ,
116
+ "childNodes" : [
117
+ {
118
+ "nodeType" : 3 ,
119
+ "nodeName" : "#text" ,
120
+ "nodeValue" : "a" ,
121
+ "childNodes" : [ ]
122
+ }
123
+ ]
124
+ } ,
125
+ {
126
+ "nodeType" : 1 ,
127
+ "tagName" : "li" ,
128
+ "childNodes" : [
129
+ {
130
+ "nodeType" : 3 ,
131
+ "nodeName" : "#text" ,
132
+ "nodeValue" : "b" ,
133
+ "childNodes" : [ ]
134
+ }
135
+ ]
136
+ } ,
137
+ {
138
+ "nodeType" : 1 ,
139
+ "tagName" : "li" ,
140
+ "childNodes" : [
141
+ {
142
+ "nodeType" : 3 ,
143
+ "nodeName" : "#text" ,
144
+ "nodeValue" : "c" ,
145
+ "childNodes" : [ ]
146
+ }
147
+ ]
146
148
}
147
149
]
148
150
}
149
151
]
150
- }
151
- ]
152
- }` ;
152
+ } ;
153
153
} ) ;
154
154
155
- describe ( "the toJSON function," , function ( ) {
156
- it ( "takes an HTML node and returns an accurate string of JSON" , function ( ) {
157
- const result = plug . toJSON ( node ) ;
158
- // Ensure we get a string...
159
- expect ( result ) . toBeInstanceOf ( String ) ;
160
- // that is valid JSON...
161
- const fromJSON = JSON . parse ( result ) ;
162
- // of the expected shape.
163
- const expectedObject = JSON . parse ( jsonString ) ;
164
- expect ( fromJSON ) . toEqual ( expectedObject ) ;
155
+ describe ( "the nodeToJSON function," , function ( ) {
156
+ it ( "takes an HTML node and returns a (JSON serializable) JS object" , function ( ) {
157
+ const result = plug . nodeToJS ( node ) ;
158
+ // is JS that's serializable to JSON...
159
+ const jsonResult = JSON . stringify ( result ) ;
160
+ // and of the expected shape.
161
+ expect ( result ) . toEqual ( jsRepresentation ) ;
165
162
} ) ;
166
163
} ) ;
167
164
168
- describe ( "the toDOM function," , function ( ) {
169
- it ( "takes a JSON string and returns the expected DOM fragment" , function ( ) {
170
- const domResult = plug . toDOM ( jsonString ) ;
171
- // Because the JSON serialization removes extraneous things like
172
- // newlines (so the node object and domResult won't be the same), just
173
- // re-serialize to JSON to check they're the same.
174
- const jsonResult = plug . toJSON ( domResult ) ;
175
- const expectedObject = plug . toJSON ( node ) ;
176
- expect ( jsonResult ) . toEqual ( expectedObject ) ;
165
+ describe ( "the jsToNode function," , function ( ) {
166
+ it ( "takes a JS object and returns the expected DOM fragment" , function ( ) {
167
+ const domResult = plug . jsToNode ( jsRepresentation ) ;
168
+ const jsonResult = plug . nodeToJS ( domResult ) ;
169
+ expect ( jsonResult ) . toEqual ( jsRepresentation ) ;
177
170
} ) ;
178
171
} ) ;
179
172
} ) ;
@@ -229,21 +222,21 @@ describe("When working with PolyPlug,", function() {
229
222
} ) ;
230
223
231
224
it ( "the oldNode is mutated to the newNode" , function ( ) {
232
- const expectedJSON = plug . toJSON ( newNode ) ;
233
- // The JSON serialization for both old and new are different.
234
- expect ( plug . toJSON ( oldNode ) ) . not . toEqual ( expectedJSON ) ;
225
+ const expectedJS = plug . nodeToJS ( newNode ) ;
226
+ // The JS serialization for both old and new are different.
227
+ expect ( plug . nodeToJS ( oldNode ) ) . not . toEqual ( expectedJS ) ;
235
228
// Mutate.
236
229
const is_changed = plug . mutate ( oldNode , newNode ) ;
237
230
// Confirmation the oldNode has changed.
238
231
expect ( is_changed ) . toEqual ( true ) ;
239
232
// The JSON serialization for both old and new are now the same.
240
- expect ( plug . toJSON ( oldNode ) ) . toEqual ( expectedJSON ) ;
233
+ expect ( plug . nodeToJS ( oldNode ) ) . toEqual ( expectedJS ) ;
241
234
} ) ;
242
235
243
236
it ( "the oldNode isn't changed if newNode is the same" , function ( ) {
244
- const expectedJSON = plug . toJSON ( oldNode ) ;
245
- const nodeA = plug . toDOM ( expectedJSON ) ;
246
- const nodeB = plug . toDOM ( expectedJSON ) ;
237
+ const expectedJS = plug . nodeToJS ( oldNode ) ;
238
+ const nodeA = plug . jsToNode ( expectedJS ) ;
239
+ const nodeB = plug . jsToNode ( expectedJS ) ;
247
240
// Mutate
248
241
const is_changed = plug . mutate ( nodeA , nodeB ) ;
249
242
// No change.
@@ -347,9 +340,8 @@ describe("When working with PolyPlug,", function() {
347
340
plug . receiveMessage ( msg ) ;
348
341
// The DOM has been updated as expected.
349
342
const updatedDIV = plug . getElements ( { id : "testMutate" } ) [ 0 ] ;
350
- const actual = plug . toJSON ( updatedDIV ) ;
351
- const expected = JSON . stringify ( target ) ;
352
- expect ( actual ) . toEqual ( expected ) ;
343
+ const actual = plug . nodeToJS ( updatedDIV ) ;
344
+ expect ( actual ) . toEqual ( target ) ;
353
345
} ) ;
354
346
it ( "the registerEvent message registers an event" , function ( done ) {
355
347
const msg = JSON . stringify ( {
0 commit comments