Add tests + Travis

This commit is contained in:
Tobe O 2018-07-09 13:20:14 -04:00
parent d997734f0f
commit 67a62e751f
5 changed files with 159 additions and 2 deletions

4
.travis.yml Normal file
View file

@ -0,0 +1,4 @@
language: dart
dart:
- dev
- stable

View file

@ -1,4 +1,7 @@
# seo
[![Pub](https://img.shields.io/pub/v/angel_seo.svg)](https://pub.dartlang.org/packages/angel_seo)
[![build status](https://travis-ci.org/angel-dart/seo.svg?branch=master)](https://travis-ci.org/angel-dart/seo)
Helpers for building SEO-friendly Web pages in Angel. The goal of
`package:angel_seo` is to speed up perceived client page loads, prevent
the infamous

View file

@ -1 +1 @@
export 'src/inline_assets.dart';
export 'src/inline_assets.dart';

View file

@ -1,9 +1,17 @@
name: angel_seo
description: Helpers for building SEO-friendly Web pages in Angel.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/seo
version: 1.0.0
environment:
sdk: ">=1.8.0 <3.0.0"
dependencies:
angel_framework: ^1.0.0-dev
angel_static: ^1.3.0
dart2_constant: ^1.0.0
file: ^2.0.0
html: ^0.13.0
path: ^1.0.0
dev_dependencies:
angel_static: ^1.3.0
angel_test: ^1.0.0
test: ^0.12.0

View file

@ -0,0 +1,142 @@
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_seo/angel_seo.dart';
import 'package:angel_static/angel_static.dart';
import 'package:angel_test/angel_test.dart';
import 'package:dart2_constant/convert.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:html/dom.dart' as html;
import 'package:html/parser.dart' as html;
import 'package:test/test.dart';
void main() {
group('inlineAssets', () {
group('buffer', inlineAssetsTests((app, dir) {
app.get('/', (ResponseContext res) async {
var indexHtml = dir.childFile('index.html');
var contents = await indexHtml.readAsString();
res
..headers['content-type'] = 'text/html; charset=utf-8'
..buffer.add(utf8.encode(contents));
});
app.responseFinalizers.add(inlineAssets(dir));
}));
group('virtual_directory', inlineAssetsTests((app, dir) {
var vDir = inlineAssetsFromVirtualDirectory(
new VirtualDirectory(app, dir.fileSystem, source: dir));
app.use(vDir.handleRequest);
}));
});
}
void Function() inlineAssetsTests(void Function(Angel, Directory) f) {
return () {
TestClient client;
setUp(() async {
var app = new Angel();
var fs = new MemoryFileSystem();
var dir = fs.currentDirectory;
f(app, dir);
client = await connectTo(app);
for (var path in contents.keys) {
var file = fs.file(path);
await file.writeAsString(contents[path].trim());
}
});
tearDown(() => client.close());
group('sends html', () {
html.Document doc;
setUp(() async {
var res = await client.get('/', headers: {'accept': 'text/html'});
doc = html.parse(res.body);
});
group('stylesheets', () {
test('replaces <link> with <style>', () {
expect(doc.querySelectorAll('link'), hasLength(1));
});
test('populates a <style>', () {
var style = doc.querySelector('style');
expect(style.innerHtml.trim(), contents['site.css']);
});
test('heeds data-no-inline', () {
var link = doc.querySelector('link');
expect(link.attributes, containsPair('rel', 'stylesheet'));
expect(link.attributes, containsPair('href', 'not-inlined.css'));
expect(link.attributes.keys, isNot(contains('data-no-inline')));
});
test('preserves other attributes', () {
var link = doc.querySelector('link');
expect(link.attributes, containsPair('data-foo', 'bar'));
});
});
group('scripts', () {
test('does not replace <script> with anything', () {
expect(doc.querySelectorAll('script'), hasLength(2));
});
test('populates innerHtml', () {
var script0 = doc.querySelectorAll('script')[0];
expect(script0.innerHtml.trim(), contents['site.js']);
});
test('heeds data-no-inline', () {
var script1 = doc.querySelectorAll('script')[1];
expect(script1.attributes, containsPair('src', 'not-inlined.js'));
expect(script1.attributes.keys, isNot(contains('data-no-inline')));
});
test('preserves other attributes', () {
var script0 = doc.querySelectorAll('script')[0];
var script1 = doc.querySelectorAll('script')[1];
expect(script0.attributes, containsPair('data-foo', 'bar'));
expect(script1.attributes, containsPair('type', 'text/javascript'));
});
});
});
};
}
var contents = <String, String>{
'index.html': '''<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="site.css">
<link data-foo="bar" rel="stylesheet" href="not-inlined.css" data-no-inline>
<script data-foo="bar" src="site.js"></script>
<script type="text/javascript" src="not-inlined.js" data-no-inline></script>
<title>Angel SEO</title>
</head>
<body>
<h1>Angel SEO</h1>
<p>Embrace the power of inlined styles, etc.</p>
</body>
</html>''',
'not-inlined.css': '''p {
font-style: italic;
}''',
'not-inlined.js': '''window.addEventListener('load', function() {
console.log('THIS message was not from an inlined file.');
});''',
'site.css': '''h1 {
color: pink;
}''',
'site.js': '''window.addEventListener('load', function() {
console.log('Hello, inline world!');
});'''
};