Skip to content

Commit

Permalink
Split up tests in API and CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva committed Jun 9, 2015
1 parent 46284aa commit d78dd97
Show file tree
Hide file tree
Showing 3 changed files with 405 additions and 397 deletions.
269 changes: 269 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
'use strict';
var fs = require('fs');
var test = require('ava');
var imageSize = require('image-size');
var concatStream = require('concat-stream');
var easydate = require('easydate');
var PNG = require('png-js');
var Pageres = require('../');
var Server = require('./serverForCookieTests');

process.chdir(__dirname);

test('expose a constructor', function (t) {
t.plan(1);
t.assert(typeof Pageres === 'function');
});

test('add a source', function (t) {
t.plan(1);

var pageres = new Pageres()
.src('yeoman.io', ['1280x1024', '1920x1080']);

t.assert(pageres._src[0].url === 'yeoman.io');
});

test('set destination directory', function (t) {
t.plan(1);

var pageres = new Pageres()
.dest('tmp');

t.assert(pageres._dest === 'tmp');
});

test('generate screenshots', function (t) {
t.plan(5);

var pageres = new Pageres()
.src('yeoman.io', ['480x320', '1024x768', 'iphone 5s'])
.src('todomvc.com', ['1280x1024', '1920x1080']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams.length === 5);
t.assert(streams[0].filename === 'todomvc.com-1280x1024.png');
t.assert(streams[4].filename === 'yeoman.io-320x568.png');

streams[0].once('data', function (data) {
t.assert(data.length > 1000);
});
});
});

test('remove special characters from the URL to create a valid filename', function (t) {
t.plan(3);

var pageres = new Pageres()
.src('http://www.microsoft.com/?query=pageres*|<>:"\\', ['1024x768']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams.length === 1);
t.assert(streams[0].filename === 'microsoft.com!query=pageres-1024x768.png');
});
});

test('have a `delay` option', function (t) {
t.plan(2);

var pageres = new Pageres({delay: 2})
.src('http://todomvc.com', ['1024x768']);

pageres.run(function (err, streams) {
t.assert(!err, err);

var now = new Date();

streams[0].once('data', function () {
t.assert((new Date()) - now > 2000);
});
});
});

test('crop image using the `crop` option', function (t) {
t.plan(4);

var pageres = new Pageres({crop: true})
.src('http://todomvc.com', ['1024x768']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams[0].filename === 'todomvc.com-1024x768-cropped.png');

streams[0].pipe(concatStream(function (data) {
var size = imageSize(data);

t.assert(size.width === 1024);
t.assert(size.height === 768);
}));
});
});

test('rename image using the `filename` option', function (t) {
t.plan(3);

var pageres = new Pageres()
.src('http://todomvc.com', ['1024x768'], {filename: '<%= date %> - <%= time %> - <%= url %>'});

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams.length === 1);
t.assert(streams[0].filename === easydate('Y-M-d') + ' - ' + easydate('h-m-s') + ' - todomvc.com.png');
});
});

test('capture a DOM element using the `selector` option', function (t) {
t.plan(4);

var pageres = new Pageres({selector: '.page-header'})
.src('http://yeoman.io', ['1024x768']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams[0].filename === 'yeoman.io-1024x768.png');

streams[0].pipe(concatStream(function (data) {
var size = imageSize(data);

t.assert(size.width === 1024);
t.assert(size.height === 80);
}));
});
});

test('support local relative files', function (t) {
t.plan(3);

var pageres = new Pageres()
.src('fixture.html', ['1024x768']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams[0].filename === 'fixture.html-1024x768.png');

streams[0].once('data', function (data) {
t.assert(data.length > 1000);
});
});
});

test('fetch resolutions from w3counter', function (t) {
t.plan(3);

var pageres = new Pageres()
.src('yeoman.io', ['w3counter']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams.length === 10);

streams[0].once('data', function (data) {
t.assert(data.length > 1000);
});
});
});

test('save image', function (t) {
t.plan(3);

var pageres = new Pageres()
.src('http://todomvc.com', ['1024x768'])
.dest(__dirname);

pageres.run(function (err) {
t.assert(!err, err);
t.assert(fs.existsSync('todomvc.com-1024x768.png'));

fs.unlink('todomvc.com-1024x768.png', function (err) {
t.assert(!err, err);
});
});
});

test('remove temporary files on error', function (t) {
t.plan(4);

var pageres = new Pageres()
.src('this-is-a-error-site.io', ['1024x768'])
.dest(__dirname);

pageres.run(function (err) {
t.assert(err);
t.assert(err.message === 'Couldn\'t load url: http://this-is-a-error-site.io');

fs.readdir(__dirname, function (err, files) {
t.assert(!err, err);
t.assert(files.indexOf('this-is-a-error-site.io.png') === -1);
});
});
});

test('auth using username and password', function (t) {
t.plan(3);

var pageres = new Pageres({username: 'user', password: 'passwd'})
.src('httpbin.org/basic-auth/user/passwd', ['120x120']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams.length === 1);

streams[0].once('data', function (data) {
t.assert(data.length);
});
});
});

test('scale webpage using the `scale` option', function (t) {
t.plan(3);

var pageres = new Pageres({scale: 2, crop: true})
.src('yeoman.io', ['120x120']);

pageres.run(function (err, streams) {
t.assert(!err, err);

streams[0].pipe(concatStream(function (data) {
var size = imageSize(data);

t.assert(size.width === 240);
t.assert(size.height === 240);
}));
});
});

function cookieTest(port, input, t) {
t.plan(6);
var server = new Server(port);
var filename = 'localhost!' + port + '-320x480.png';

var pageres = new Pageres({cookies: [input]})
.src('http://localhost:' + port, ['320x480']);

pageres.run(function (err, streams) {
t.assert(!err, err);
t.assert(streams[0].filename === filename);

streams[0].pipe(concatStream(function (data) {
server.close();
var png = new PNG(data);

png.decode(function (pixels) {
t.assert(pixels[0] === 0);
t.assert(pixels[1] === 0);
t.assert(pixels[2] === 0);
t.assert(pixels[3] === 255);
});
}));
});
}

test('send cookie', cookieTest.bind(null, 5000, 'pageresColor=black; Path=/; Domain=localhost'));

test('send cookie using an object', cookieTest.bind(null, 5001, {
name: 'pageresColor',
value: 'black',
domain: 'localhost'
}));
Loading

0 comments on commit d78dd97

Please sign in to comment.