Skip to content

Commit

Permalink
✅ Fix all unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 8, 2024
1 parent 2ebdabb commit 3864d4d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 24 deletions.
12 changes: 11 additions & 1 deletion test/00-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ global.setLocation = async function setLocation(path) {
}
};

global.getDocumentHTML = async function getDocumentHTML() {
return await evalPage(function() {
return document.documentElement.outerHTML;
});
};

global.evalPage = function evalPage(fnc, ...args) {
return page.evaluate(fnc, ...args);
};
Expand Down Expand Up @@ -178,6 +184,10 @@ global.queryElementData = async function queryElementData(query) {
let result = await evalPage(function(query) {
let block = document.querySelector(query);

if (!block) {
return false;
}

let result = {
html : block.outerHTML,
text : block.textContent,
Expand Down Expand Up @@ -352,7 +362,7 @@ describe('Alchemy', function() {
it('should start the server', function(done) {

alchemy.setSetting('network.port', 3470);
alchemy.setSetting('network.postpone_requests_on_overload', false);
alchemy.setSetting('performance.postpone_requests_on_overload', false);

STAGES.getStage('datasource').addPostTask(() => {
Datasource.create('mongo', 'default', {uri: mongo_uri});
Expand Down
2 changes: 1 addition & 1 deletion test/03-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ describe('Model', function() {
doc.number = 3;
await doc.save();

assert.strictEqual(doc.initials, undefined);
assert.strictEqual(doc.initials, null);

doc.initials = 'RVG';
await doc.save();
Expand Down
9 changes: 4 additions & 5 deletions test/04-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ describe('Field.Decimal', function() {
let DecimalTester,
DecimalInSubSchema;

before(function(next) {
next = Function.regulate(next);
before(function(done) {
done = Function.regulate(done);

DecimalTester = Function.inherits('Alchemy.Model', 'DecimalTester');
DecimalInSubSchema = Function.inherits('Alchemy.Model', 'DecimalInSubSchema');
Expand All @@ -690,7 +690,7 @@ describe('Field.Decimal', function() {
next();
});

}, function secondModel(next) {
}, async function secondModel(next) {

DecimalInSubSchema.constitute(function addFields() {
this.addField('name', 'String');
Expand All @@ -702,8 +702,7 @@ describe('Field.Decimal', function() {
this.addField('decimals', schema, {is_array: true});
next();
});

}, next);
}, done);
});

it('should store decimal fields in the database', async function() {
Expand Down
10 changes: 5 additions & 5 deletions test/05-criteria.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var assert = require('assert'),
Person;

function makeCriteria() {
return new Classes.Alchemy.Criteria();
return new Classes.Alchemy.Criteria.Model();
}

describe('Criteria', function() {
Expand All @@ -26,9 +26,9 @@ describe('Criteria', function() {

assert.strictEqual(!!record._id, true, 'The _id should always be selected');
assert.strictEqual(typeof record.firstname, 'string');
assert.strictEqual(typeof record.lastname, 'undefined');
assert.strictEqual(typeof record.birthdate, 'undefined');
assert.strictEqual(typeof record.male, 'undefined');
assert.strictEqual(record.lastname, null);
assert.strictEqual(record.birthdate, null);
assert.strictEqual(record.male, null);
assert.strictEqual(typeof record.Parent, 'undefined');
assert.strictEqual(typeof records[1].Parent, 'undefined');
});
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('Criteria', function() {
assert.strictEqual(!!record.Parent, true, 'The Parent record wasn\'t selected');
assert.strictEqual(!!record.Parent._id, true, 'The Parent should have an _id field');
assert.strictEqual(record.Parent.firstname, 'Griet', 'The wrong record was selected as Parent');
assert.strictEqual(typeof record.Parent.lastname, 'undefined', 'The parent record should only have an _id and firstname field');
assert.strictEqual(record.Parent.lastname, null, 'The parent record should only have an _id and firstname field');
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/06-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ describe('Document', function() {

assert.strictEqual(child.firstname, 'Jelle');
assert.strictEqual(child.lastname, 'De Loecker');
assert.strictEqual(child.male, undefined);
assert.deepStrictEqual(child.nicknames, undefined);
assert.strictEqual(child.male, null);
assert.deepStrictEqual(child.nicknames, []);

let jelle = await Model.get('Person').findByValues({firstname: 'Jelle'});

Expand All @@ -144,7 +144,7 @@ describe('Document', function() {
await clone.populate(['Parent.firstname', 'Parent.male']);

assert.strictEqual(clone.Parent.firstname, 'Griet');
assert.strictEqual(clone.Parent.lastname, undefined);
assert.strictEqual(clone.Parent.lastname, null);
assert.strictEqual(clone.Parent.male, false);
});
});
Expand Down
17 changes: 11 additions & 6 deletions test/25-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,14 @@ describe('Controller', function() {
});

describe('#beforeAction(name)', function() {
it('should be called before an action is called', async function() {

let last_called,
calls = 0,
pledge = new Blast.Classes.Pledge();
let last_called,
calls = 0,
pledge;

it('should be called before an action is called (direct)', async function() {

pledge = new Blast.Classes.Pledge();

PersonController.setMethod(function beforeAction(name) {
last_called = name;
Expand All @@ -238,14 +241,16 @@ describe('Controller', function() {

assert.strictEqual(last_called, 'rendertest');
assert.strictEqual(calls, 1);
});

result = await openHeUrl('/render_test?view=body');
it('should be called before an action is called (ajax)', async function() {

let result = await openHeUrl('/render_test?view=body');

assert.strictEqual(result.location, '/render_test');

assert.strictEqual(last_called, 'rendertest');
assert.strictEqual(calls, 2);

});
});
});
12 changes: 11 additions & 1 deletion test/26-conduit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('Controller', function() {
});

describe('#body', function() {
it('should parse the body of any encoding type', async function() {

before(() => {
Router.add({
name : 'ConduitTest#bodyTest',
paths : '/conduit/body_test',
Expand Down Expand Up @@ -49,13 +49,20 @@ describe('Controller', function() {

this.render('static/conduit_body_test');
});
});

it('should parse the body of a url-encoded form', async function() {
post_pledge = new Pledge();
await testFormSubmission(post_pledge, 'application/x-www-form-urlencoded');

});

it('should parse the body of a multipart form', async function() {
post_pledge = new Pledge();
await testFormSubmission(post_pledge, 'multipart/form-data');
});

it('should parse the body of a json-encoded form', async function() {
post_pledge = new Pledge();
await testFormSubmission(post_pledge, 'json');
});
Expand Down Expand Up @@ -116,6 +123,9 @@ async function testFormSubmission(post_pledge, enctype) {
// Use Puppeteer for normal form submissions
await setLocation(actual_url);

let html = await getDocumentHTML();
console.log(html)

await queryElementData('.form-wrapper');

await setElementValueOrThrow('#firstname', 'Jelle');
Expand Down
8 changes: 6 additions & 2 deletions test/40-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ describe('Helper.Router', function() {
return next(err);
}

assert.strictEqual(res, `<a href="/api/test"></a>`);
done();
try {
assert.strictEqual(res, `<a href="/api/test"></a>`);
done();
} catch (err) {
done(err);
}
});
});
});
Expand Down

0 comments on commit 3864d4d

Please sign in to comment.