Skip to content

Commit

Permalink
modernize a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
boutell committed Sep 28, 2021
1 parent 0d42350 commit a74481a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 96 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"@sailshq/lodash": "^3.10.3",
"async": "^1.5.2",
"moment": "^2.24.0"
"moment": "^2.24.0",
"node-fetch": "^2.6.5"
},
"devDependencies": {
"apostrophe": "^2.85.0",
Expand Down
20 changes: 20 additions & 0 deletions test/lib/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fetch = require('node-fetch');

// Optional callback for older tests
module.exports = async function get(url, callback) {
if (callback) {
try {
const result = await body();
return callback(null, result);
} catch (e) {
return callback(e);
}
} else {
return body();
}
async function body() {
const response = await fetch(url);
return response.text();
}
};

125 changes: 47 additions & 78 deletions test/test-rewrite-url.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
var assert = require('assert');
var _ = require('@sailshq/lodash');
var async = require('async');
var request = require('request');
var fs = require('fs');
const assert = require('assert');
const fs = require('fs');
const get = require('./lib/get');

describe('Apostrophe Sitemap: test rewrite URL', function() {

var apos;
let apos;

this.timeout(5000);

after(function(done) {
try {
require('apostrophe/test-lib/util').destroy(apos, done);
} catch (e) {
console.warn('Old version of apostrophe does not export test-lib/util library, just dropping old test db');
apos.db.dropDatabase();
setTimeout(done, 1000);
}
});

it('should be a property of the apos object', function(done) {
before(function(done) {
apos = require('apostrophe')({
testModule: true,
baseUrl: 'http://localhost:7780',
Expand Down Expand Up @@ -100,78 +88,59 @@ describe('Apostrophe Sitemap: test rewrite URL', function() {
extend: 'apostrophe-pieces-pages'
},
},
afterInit: function(callback) {
afterInit: async function(callback) {
assert(apos.modules['apostrophe-site-map']);
return callback(null);
try {
const product = Object.assign(apos.modules.products.newInstance(), {
title: 'Cheese',
slug: 'cheese'
});
await apos.modules.products.insert(apos.tasks.getReq(), product);
const product2 = Object.assign(apos.modules.products.newInstance(), {
title: 'Rocks',
slug: 'rocks',
published: false
});
await apos.modules.products.insert(apos.tasks.getReq(), product2);
await apos.docs.db.update({}, {
$set: {
trash: false,
published: true
}
}, {
multi: true
});
return callback(null);
} catch (e) {
return callback(e);
}
},
afterListen: function(err) {
done();
}
});
});

it('insert a product for test purposes', function(done) {
var product = _.assign(apos.modules.products.newInstance(), {
title: 'Cheese',
slug: 'cheese'
});
apos.modules.products.insert(apos.tasks.getReq(), product, function(err) {
assert(!err);
done();
});
});

it('make sure everything is published and out of the trash for test purposes', function(done) {
return apos.docs.db.update({}, {
$set: {
trash: false,
published: true
}
}, {
multi: true
}, function(err, count) {
assert(!err);
done();
});
});

it('insert an unpublished product for test purposes', function(done) {
var product = _.assign(apos.modules.products.newInstance(), {
title: 'Rocks',
slug: 'rocks',
published: false
});
apos.modules.products.insert(apos.tasks.getReq(), product, function(err) {
assert(!err);
done();
});
after(function(done) {
try {
require('apostrophe/test-lib/util').destroy(apos, done);
} catch (e) {
console.warn('Old version of apostrophe does not export test-lib/util library, just dropping old test db');
apos.db.dropDatabase();
setTimeout(done, 1000);
}
});

it('should generate a suitable sitemap', function(done) {
it('should generate a suitable sitemap', async function() {
this.timeout(10000);
get('http://localhost:7780/sitemap.xml', function(err, xml) {
if (err) {
console.error(err);
}
assert(!err);
assert(xml);
assert(xml.indexOf('<loc>https://public-site.com/</loc>') !== -1);
assert(xml.indexOf('<loc>https://public-site.com/tab-one</loc>') !== -1);
assert(xml.indexOf('<loc>https://public-site.com/tab-two</loc>') !== -1);
assert(xml.indexOf('<loc>https://public-site.com/tab-one/child-one</loc>') !== -1);
assert(xml.indexOf('<loc>https://public-site.com/products/cheese</loc>') !== -1);
assert(xml.indexOf('<loc>https://public-site.com/products/rocks</loc>') === -1);
done();
});
const xml = await get('http://localhost:7780/sitemap.xml');
assert(xml);
assert(xml.includes('<loc>https://public-site.com/</loc>'));
assert(xml.includes('<loc>https://public-site.com/tab-one</loc>'));
assert(xml.includes('<loc>https://public-site.com/tab-two</loc>'));
assert(xml.includes('<loc>https://public-site.com/tab-one/child-one</loc>'));
assert(xml.includes('<loc>https://public-site.com/products/cheese</loc>'));
assert(xml.includes('<loc>https://public-site.com/products/rocks</loc>'));
});

});

function get(url, callback) {
return request(url, function(err, response, body) {
if (err || (response.statusCode >= 400)) {
return callback(err || response.statusCode);
}
return callback(null, body);
});
}
10 changes: 1 addition & 9 deletions test/test-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var _ = require('@sailshq/lodash');
var async = require('async');
var request = require('request');
var fs = require('fs');
const get = require('./lib/get');

describe('Apostrophe Sitemap: simple site without workflow', function() {

Expand Down Expand Up @@ -161,12 +162,3 @@ describe('Apostrophe Sitemap: simple site without workflow', function() {
});

});

function get(url, callback) {
return request(url, function(err, response, body) {
if (err || (response.statusCode >= 400)) {
return callback(err || response.statusCode);
}
return callback(null, body);
});
}
9 changes: 1 addition & 8 deletions test/test-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var async = require('async');
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
const get = require('./lib/get');

var parkedPages = [
{
Expand Down Expand Up @@ -561,11 +562,3 @@ describe('Apostrophe Sitemap: workflow: single sitemap with hreflang alternative

});

function get(url, callback) {
return request(url, function(err, response, body) {
if (err || (response.statusCode >= 400)) {
return callback(err || response.statusCode);
}
return callback(null, body);
});
}

0 comments on commit a74481a

Please sign in to comment.