Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arttse committed Oct 12, 2016
1 parent 9e83811 commit 2720a14
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 24 deletions.
21 changes: 21 additions & 0 deletions test/custom/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ test ( 'html', t => {
'Argument «html» must be a «string», not «number»'
);

t.throws (
() => {
m ( true );
},
'Argument «html» must be a «string», not «boolean»'
);

t.throws (
() => {
m ( Symbol ( 'foo' ) );
},
'Argument «html» must be a «string», not «symbol»'
);

t.throws (
() => {
m ( () => {} );
},
'Argument «html» must be a «string», not «function»'
);

t.notThrows (
() => {
m ( 'beleberda' );
Expand Down
22 changes: 14 additions & 8 deletions test/custom/links-href.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import fs from 'fs';
import test from 'ava';
import m from '../../';

const isObj = arg => {
return arg !== null && typeof arg === 'object' && !Array.isArray ( arg );
};
const html = fs.readFileSync ( '../html/links-href.html', 'utf8' );

test ( 'default-opts', t => {

const out = m ( html );

t.true ( Array.isArray ( out ) );
t.true ( out.length === 4 );

t.is ( out[0].innerHTML, '<b>Some site</b>' );
t.is ( out[0].href, 'http://site.com' );
t.is ( out[0].text, undefined );
Expand Down Expand Up @@ -53,25 +59,25 @@ test ( 'default-opts-reverse', t => {
t.is ( out[0].href, undefined );
t.is ( out[0].text, 'Some site' );
t.deepEqual ( out[0].attributes, {href : 'http://site.com'} );
t.true ( typeof out[0].element === 'object' );
t.true ( isObj ( out[0].element ) );

t.is ( out[1].innerHTML, undefined );
t.is ( out[1].href, undefined );
t.is ( out[1].text, 'Link in page' );
t.deepEqual ( out[1].attributes, {href : '#first'} );
t.true ( typeof out[1].element === 'object' );
t.true ( isObj ( out[1].element ) );

t.is ( out[2].innerHTML, undefined );
t.is ( out[2].href, undefined );
t.is ( out[2].text, 'Hello!' );
t.deepEqual ( out[2].attributes, {href : `javascript:alert('Hello, World!');`} );
t.true ( typeof out[2].element === 'object' );
t.true ( isObj ( out[2].element ) );

t.is ( out[3].innerHTML, undefined );
t.is ( out[3].href, undefined );
t.is ( out[3].text, 'FooBar' );
t.deepEqual ( out[3].attributes, {href : '/foo/bar'} );
t.true ( typeof out[3].element === 'object' );
t.true ( isObj ( out[3].element ) );

} );

Expand All @@ -92,24 +98,24 @@ test ( 'all-opts-true', t => {
t.is ( out[0].href, 'http://site.com' );
t.is ( out[0].text, 'Some site' );
t.deepEqual ( out[0].attributes, {href : 'http://site.com'} );
t.true ( typeof out[0].element === 'object' );
t.true ( isObj ( out[0].element ) );

t.is ( out[1].innerHTML, 'Link in page' );
t.is ( out[1].href, '#first' );
t.is ( out[1].text, 'Link in page' );
t.deepEqual ( out[1].attributes, {href : '#first'} );
t.true ( typeof out[1].element === 'object' );
t.true ( isObj ( out[1].element ) );

t.is ( out[2].innerHTML, 'Hello!' );
t.is ( out[2].href, `javascript:alert('Hello, World!');` );
t.is ( out[2].text, 'Hello!' );
t.deepEqual ( out[2].attributes, {href : `javascript:alert('Hello, World!');`} );
t.true ( typeof out[2].element === 'object' );
t.true ( isObj ( out[2].element ) );

t.is ( out[3].innerHTML, 'FooBar' );
t.is ( out[3].href, '/foo/bar' );
t.is ( out[3].text, 'FooBar' );
t.deepEqual ( out[3].attributes, {href : '/foo/bar'} );
t.true ( typeof out[3].element === 'object' );
t.true ( isObj ( out[3].element ) );

} );
1 change: 1 addition & 0 deletions test/custom/no-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test ( t => {

const out = m ( html );

t.true ( Array.isArray( out ) );
t.true ( out.length === 0 );

} );
21 changes: 21 additions & 0 deletions test/get-elements/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ test ( 'html', t => {
'Argument «html» must be a «string», not «number»'
);

t.throws (
() => {
m.getElements ( true );
},
'Argument «html» must be a «string», not «boolean»'
);

t.throws (
() => {
m.getElements ( Symbol ( 'foo' ) );
},
'Argument «html» must be a «string», not «symbol»'
);

t.throws (
() => {
m.getElements ( () => {} );
},
'Argument «html» must be a «string», not «function»'
);

t.notThrows (
() => {
m.getElements ( 'beleberda' );
Expand Down
42 changes: 26 additions & 16 deletions test/get-elements/links-href.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,56 @@ import fs from 'fs';
import test from 'ava';
import m from '../../';

const isObj = arg => {
return arg !== null && typeof arg === 'object' && !Array.isArray ( arg );
};
const html = fs.readFileSync ( '../html/links-href.html', 'utf8' );

test ( t => {

const a = m.getElements ( html );

t.true ( Array.isArray ( a ) );
t.true ( a.length === 4 );

// <a href="http://site.com"><b>Some site</b></a>
t.is ( a[0].type, 'tag' );
t.is ( a[0].name, 'a' );
t.true ( isObj ( a[0].attribs ) );
t.is ( a[0].attribs.href, 'http://site.com' );
t.true ( a[0].children !== null );
t.true ( a[0].next !== null );
t.true ( a[0].prev !== null );
t.true ( a[0].parent !== null );
t.true ( Array.isArray ( a[0].children ) );
t.true ( isObj ( a[0].next ) );
t.true ( isObj ( a[0].prev ) );
t.true ( isObj ( a[0].parent ) );

// <a href="#first">Link in page</a>
t.is ( a[1].type, 'tag' );
t.is ( a[1].name, 'a' );
t.true ( isObj ( a[1].attribs ) );
t.is ( a[1].attribs.href, '#first' );
t.true ( a[1].children !== null );
t.true ( a[1].next !== null );
t.true ( a[1].prev !== null );
t.true ( a[1].parent !== null );
t.true ( Array.isArray ( a[1].children ) );
t.true ( isObj ( a[1].next ) );
t.true ( isObj ( a[1].prev ) );
t.true ( isObj ( a[1].parent ) );

// <a href="javascript:alert('Hello, World!');">Hello!</a>
t.is ( a[2].type, 'tag' );
t.is ( a[2].name, 'a' );
t.true ( isObj ( a[2].attribs ) );
t.is ( a[2].attribs.href, `javascript:alert('Hello, World!');` );
t.true ( a[2].children !== null );
t.true ( a[2].next !== null );
t.true ( a[2].prev !== null );
t.true ( a[2].parent !== null );
t.true ( Array.isArray ( a[2].children ) );
t.true ( isObj ( a[2].next ) );
t.true ( isObj ( a[2].prev ) );
t.true ( isObj ( a[2].parent ) );

// <a href="/foo/bar">FooBar</a>
t.is ( a[3].type, 'tag' );
t.is ( a[3].name, 'a' );
t.true ( isObj ( a[3].attribs ) );
t.is ( a[3].attribs.href, '/foo/bar' );
t.true ( a[3].children !== null );
t.true ( a[3].next !== null );
t.true ( a[3].prev !== null );
t.true ( a[3].parent !== null );
t.true ( Array.isArray ( a[3].children ) );
t.true ( isObj ( a[3].next ) );
t.true ( isObj ( a[3].prev ) );
t.true ( isObj ( a[3].parent ) );

} );
1 change: 1 addition & 0 deletions test/get-elements/no-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test ( t => {

const out = m.getElements ( html );

t.true ( Array.isArray ( out ) );
t.true ( out.length === 0 );

} );

0 comments on commit 2720a14

Please sign in to comment.