UNPKG

3.86 kBJavaScriptView Raw
1/*global describe, it */
2
3var flow = require('../lib/xml-flow')
4 , fs = require('fs')
5 , should = require('chai').should()
6;
7
8function getFlow(fileName, options) {
9 return flow(fs.createReadStream(fileName), options);
10}
11
12describe('xml-flow', function(){
13 describe('invoke', function(){
14 it('should create an emitter when invoked with a stream and options', function(){
15 var simpleStream = getFlow('./test/simple.xml');
16 simpleStream.on.should.be.a('function');
17 simpleStream.pause();
18 simpleStream.resume();
19 });
20 });
21
22 describe(".on('end')", function(){
23 it('should emit after the file has been read', function(done){
24 var simpleStream = getFlow('./test/simple.xml');
25 simpleStream.on('end', function(){
26 done();
27 });
28 });
29 });
30
31 describe(".on('tag:...')", function(){
32 it('should emit the right number of nodes', function(done){
33 var simpleStream = getFlow('./test/simple.xml')
34 , count = 0
35 ;
36
37 simpleStream.on('tag:item', function(){
38 count++;
39 });
40
41 simpleStream.on('end', function(){
42 count.should.equal(3);
43 done();
44 });
45 });
46
47 it('should make non-attributed data look really simple', function(done){
48 var simpleStream = getFlow('./test/test.xml')
49 , output = {
50 $name: 'no-attrs',
51 person: [
52 {name: 'Bill', id: '1', age:'27'},
53 {name: 'Joe', id: '2', age:'29'},
54 {name: 'Smitty', id: '3', age:'37'}
55 ]
56 }
57 ;
58
59 simpleStream.on('tag:no-attrs', function(node){
60 node.should.deep.equal(output);
61 done();
62 });
63 });
64
65 it('should make all-attributed data look really simple', function(done){
66 var simpleStream = getFlow('./test/test.xml')
67 , output = {
68 $name: 'all-attrs',
69 person: [
70 {name: 'Bill', id: '1', age:'27'},
71 {name: 'Joe', id: '2', age:'29'},
72 {name: 'Smitty', id: '3', age:'37'}
73 ]
74 }
75 ;
76
77 simpleStream.on('tag:all-attrs', function(node){
78 node.should.deep.equal(output);
79 done();
80 });
81 });
82
83 it('should handle tags with both attributes and other stuff', function(done){
84 var simpleStream = getFlow('./test/test.xml')
85 , output = {
86 $name: 'mixed',
87 person: [
88 {$attrs:{name: 'Bill', id: '1', age:'27'}, $text: 'some text'},
89 {$attrs: {name: 'Joe', id: '2', age:'29'}, p: 'some paragraph'},
90 {$attrs: {name: 'Smitty', id: '3', age:'37'}, thing: {id:'999', ref:'blah'}}
91 ]
92 }
93 ;
94
95 simpleStream.on('tag:mixed', function(node){
96 node.should.deep.equal(output);
97 done();
98 });
99 });
100
101 it('should handle scripts', function(done){
102 var simpleStream = getFlow('./test/test.xml')
103 , output = {
104 $name: 'has-scripts',
105 script: [
106 'var x = 3;',
107 {$attrs: {type: 'text/javascript'}, $script: '//this is a comment'}
108 ]
109 }
110 ;
111
112 simpleStream.on('tag:has-scripts', function(node){
113 node.should.deep.equal(output);
114 done();
115 });
116 });
117 });
118});