sn-models package

This commit is contained in:
Mo Bitar
2018-06-30 10:13:55 -05:00
parent b4902f03c5
commit 71a3c68d39
19 changed files with 4422 additions and 594 deletions

View File

@@ -70,9 +70,9 @@ module.exports = function(grunt) {
},
app: {
src: [
'node_modules/sn-models/dist/sn-models.js',
'app/assets/javascripts/app/*.js',
'app/assets/javascripts/app/controllers/**/*.js',
'app/assets/javascripts/app/models/**/*.js',
'app/assets/javascripts/app/services/**/*.js',
'app/assets/javascripts/app/filters/**/*.js',
'app/assets/javascripts/app/directives/**/*.js',
@@ -161,9 +161,6 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-babel');
grunt.loadNpmTasks('grunt-browserify');
// grunt.registerTask('default', ['haml', 'ngtemplates', 'sass', 'concat:app',
// 'concat:lib', 'concat:dist', 'concat:css', 'babel', 'browserify', 'uglify']);
grunt.registerTask('default', ['haml', 'ngtemplates', 'sass', 'concat:app', 'babel', 'browserify',
'concat:lib', 'concat:dist', 'ngAnnotate', 'concat:css', 'uglify']);
};

View File

@@ -1,28 +0,0 @@
class Mfa extends SFItem {
constructor(json_obj) {
super(json_obj);
}
// mapContentToLocalProperties(content) {
// super.mapContentToLocalProperties(content)
// this.serverContent = content;
// }
//
// structureParams() {
// return _.merge(this.serverContent, super.structureParams());
// }
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SF|MFA";
}
doNotEncrypt() {
return true;
}
}

View File

@@ -1,38 +0,0 @@
class ServerExtension extends SFItem {
constructor(json_obj) {
super(json_obj);
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.url = content.url;
}
// structureParams() {
// // There was a bug with the way Base64 content was parsed in previous releases related to this item.
// // The bug would not parse the JSON behind the base64 string and thus saved data in an invalid format.
// // This is the line: https://github.com/standardnotes/web/commit/1ad0bf73d8e995b7588854f1b1e4e4a02303a42f#diff-15753bac364782a3a5876032bcdbf99aR76
// // We'll remedy this for affected users by trying to parse the content string
// if(typeof this.content !== 'object') {
// try {
// this.content = JSON.parse(this.content);
// } catch (e) {}
// }
// var params = this.content || {};
// _.merge(params, super.structureParams());
// return params;
// }
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SF|Extension";
}
doNotEncrypt() {
return true;
}
}

View File

@@ -1,147 +0,0 @@
class Component extends SFItem {
constructor(json_obj) {
// If making a copy of an existing component (usually during sign in if you have a component active in the session),
// which may have window set, you may get a cross-origin exception since you'll be trying to copy the window. So we clear it here.
json_obj.window = null;
super(json_obj);
if(!this.componentData) {
this.componentData = {};
}
if(!this.disassociatedItemIds) {
this.disassociatedItemIds = [];
}
if(!this.associatedItemIds) {
this.associatedItemIds = [];
}
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
/* Legacy */
this.url = content.url || content.hosted_url;
/* New */
this.local_url = content.local_url;
this.hosted_url = content.hosted_url || content.url;
this.offlineOnly = content.offlineOnly;
if(content.valid_until) {
this.valid_until = new Date(content.valid_until);
}
this.name = content.name;
this.autoupdateDisabled = content.autoupdateDisabled;
this.package_info = content.package_info;
// the location in the view this component is located in. Valid values are currently tags-list, note-tags, and editor-stack`
this.area = content.area;
this.permissions = content.permissions;
if(!this.permissions) {
this.permissions = [];
}
this.active = content.active;
// custom data that a component can store in itself
this.componentData = content.componentData || {};
// items that have requested a component to be disabled in its context
this.disassociatedItemIds = content.disassociatedItemIds || [];
// items that have requested a component to be enabled in its context
this.associatedItemIds = content.associatedItemIds || [];
}
handleDeletedContent() {
super.handleDeletedContent();
this.active = false;
}
structureParams() {
var params = {
url: this.url,
hosted_url: this.hosted_url,
local_url: this.local_url,
valid_until: this.valid_until,
offlineOnly: this.offlineOnly,
name: this.name,
area: this.area,
package_info: this.package_info,
permissions: this.permissions,
active: this.active,
autoupdateDisabled: this.autoupdateDisabled,
componentData: this.componentData,
disassociatedItemIds: this.disassociatedItemIds,
associatedItemIds: this.associatedItemIds,
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SN|Component";
}
isEditor() {
return this.area == "editor-editor";
}
isTheme() {
return this.content_type == "SN|Theme" || this.area == "themes";
}
isDefaultEditor() {
return this.getAppDataItem("defaultEditor") == true;
}
setLastSize(size) {
this.setAppDataItem("lastSize", size);
}
getLastSize() {
return this.getAppDataItem("lastSize");
}
keysToIgnoreWhenCheckingContentEquality() {
return ["active"].concat(super.keysToIgnoreWhenCheckingContentEquality());
}
/*
An associative component depends on being explicitly activated for a given item, compared to a dissaciative component,
which is enabled by default in areas unrelated to a certain item.
*/
static associativeAreas() {
return ["editor-editor"];
}
isAssociative() {
return Component.associativeAreas().includes(this.area);
}
associateWithItem(item) {
this.associatedItemIds.push(item.uuid);
}
isExplicitlyEnabledForItem(item) {
return this.associatedItemIds.indexOf(item.uuid) !== -1;
}
isExplicitlyDisabledForItem(item) {
return this.disassociatedItemIds.indexOf(item.uuid) !== -1;
}
}

View File

@@ -1,103 +0,0 @@
class Editor extends SFItem {
constructor(json_obj) {
super(json_obj);
if(!this.notes) {
this.notes = [];
}
if(!this.data) {
this.data = {};
}
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.url = content.url;
this.name = content.name;
this.data = content.data || {};
this.default = content.default;
this.systemEditor = content.systemEditor;
}
structureParams() {
var params = {
url: this.url,
name: this.name,
data: this.data,
default: this.default,
systemEditor: this.systemEditor
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
referenceParams() {
var references = _.map(this.notes, function(note){
return {uuid: note.uuid, content_type: note.content_type};
})
return references;
}
addItemAsRelationship(item) {
if(item.content_type == "Note") {
if(!_.find(this.notes, item)) {
this.notes.push(item);
}
}
super.addItemAsRelationship(item);
}
removeItemAsRelationship(item) {
if(item.content_type == "Note") {
_.pull(this.notes, item);
}
super.removeItemAsRelationship(item);
}
removeAndDirtyAllRelationships() {
super.removeAndDirtyAllRelationships();
this.notes = [];
}
removeReferencesNotPresentIn(references) {
super.removeReferencesNotPresentIn(references);
var uuids = references.map(function(ref){return ref.uuid});
this.notes.forEach(function(note){
if(!uuids.includes(note.uuid)) {
_.remove(this.notes, {uuid: note.uuid});
}
}.bind(this))
}
potentialItemOfInterestHasChangedItsUUID(newItem, oldUUID, newUUID) {
if(newItem.content_type === "Note" && _.find(this.notes, {uuid: oldUUID})) {
_.remove(this.notes, {uuid: oldUUID});
this.notes.push(newItem);
}
}
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SN|Editor";
}
setData(key, value) {
var dataHasChanged = JSON.stringify(this.data[key]) !== JSON.stringify(value);
if(dataHasChanged) {
this.data[key] = value;
return true;
}
return false;
}
dataForKey(key) {
return this.data[key] || {};
}
}

View File

@@ -1,62 +0,0 @@
class Action {
constructor(json) {
_.merge(this, json);
this.running = false; // in case running=true was synced with server since model is uploaded nondiscriminatory
this.error = false;
if(this.lastExecuted) {
// is string
this.lastExecuted = new Date(this.lastExecuted);
}
}
}
class Extension extends Component {
constructor(json) {
super(json);
if(json.actions) {
this.actions = json.actions.map(function(action){
return new Action(action);
})
}
if(!this.actions) {
this.actions = [];
}
}
actionsWithContextForItem(item) {
return this.actions.filter(function(action){
return action.context == item.content_type || action.context == "Item";
})
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.description = content.description;
this.supported_types = content.supported_types;
if(content.actions) {
this.actions = content.actions.map(function(action){
return new Action(action);
})
}
}
get content_type() {
return "Extension";
}
structureParams() {
var params = {
description: this.description,
actions: this.actions.map((a) => {return _.omit(a, ["subrows", "subactions"])}),
supported_types: this.supported_types
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
}

View File

@@ -1,107 +0,0 @@
export class Note extends SFItem {
constructor(json_obj) {
super(json_obj);
if(!this.text) {
// Some external editors can't handle a null value for text.
// Notes created on mobile with no text have a null value for it,
// so we'll just set a default here.
this.text = "";
}
if(!this.tags) {
this.tags = [];
}
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.title = content.title;
this.text = content.text;
}
structureParams() {
var params = {
title: this.title,
text: this.text
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
addItemAsRelationship(item) {
this.savedTagsString = null;
if(item.content_type == "Tag") {
if(!_.find(this.tags, {uuid: item.uuid})) {
this.tags.push(item);
item.notes.push(this);
}
}
super.addItemAsRelationship(item);
}
removeItemAsRelationship(item) {
this.savedTagsString = null;
if(item.content_type == "Tag") {
_.remove(this.tags, {uuid: item.uuid});
_.remove(item.notes, {uuid: this.uuid});
}
super.removeItemAsRelationship(item);
}
updateLocalRelationships() {
this.savedTagsString = null;
var references = this.content.references;
var uuids = references.map(function(ref){return ref.uuid});
this.tags.slice().forEach(function(tag){
if(!uuids.includes(tag.uuid)) {
_.remove(tag.notes, {uuid: this.uuid});
_.remove(this.tags, {uuid: tag.uuid});
}
}.bind(this))
}
isBeingRemovedLocally() {
this.tags.forEach(function(tag){
_.remove(tag.notes, {uuid: this.uuid});
}.bind(this))
super.isBeingRemovedLocally();
}
static filterDummyNotes(notes) {
var filtered = notes.filter(function(note){return note.dummy == false || note.dummy == null});
return filtered;
}
informReferencesOfUUIDChange(oldUUID, newUUID) {
super.informReferencesOfUUIDChange();
for(var tag of this.tags) {
_.remove(tag.notes, {uuid: oldUUID});
tag.notes.push(this);
}
}
safeText() {
return this.text || "";
}
safeTitle() {
return this.title || "";
}
toJSON() {
return {uuid: this.uuid}
}
tagsString() {
this.savedTagsString = Tag.arrayToDisplayString(this.tags);
return this.savedTagsString;
}
}

View File

@@ -1,45 +0,0 @@
export class Tag extends SFItem {
constructor(json_obj) {
super(json_obj);
if(!this.notes) {
this.notes = [];
}
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.title = content.title;
}
structureParams() {
var params = {
title: this.title
};
var superParams = super.structureParams();
Object.assign(superParams, params);
return superParams;
}
isBeingRemovedLocally() {
this.notes.forEach(function(note){
_.remove(note.tags, {uuid: this.uuid});
}.bind(this))
super.isBeingRemovedLocally();
}
informReferencesOfUUIDChange(oldUUID, newUUID) {
for(var note of this.notes) {
_.remove(note.tags, {uuid: oldUUID});
note.tags.push(this);
}
}
static arrayToDisplayString(tags) {
return tags.sort((a, b) => {return a.title > b.title}).map(function(tag, i){
return "#" + tag.title;
}).join(" ");
}
}

View File

@@ -1,11 +0,0 @@
class Theme extends Component {
constructor(json_obj) {
super(json_obj);
this.area = "themes";
}
get content_type() {
return "SN|Theme";
}
}

View File

@@ -1,28 +0,0 @@
class EncryptedStorage extends SFItem {
constructor(json_obj) {
super(json_obj);
}
mapContentToLocalProperties(content) {
super.mapContentToLocalProperties(content)
this.storage = content.storage;
}
structureParams() {
var params = {
storage: this.storage,
};
_.merge(params, super.structureParams());
return params;
}
toJSON() {
return {uuid: this.uuid}
}
get content_type() {
return "SN|EncryptedStorage";
}
}

View File

@@ -33,6 +33,7 @@ class ModelManager extends SFModelManager {
var tag = _.find(this.tags, {title: title})
if(!tag) {
tag = this.createItem({content_type: "Tag", content: {title: title}});
tag.setDirty(true);
this.addItem(tag);
}
return tag;

3630
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -37,7 +37,12 @@
"karma-jasmine": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.2",
"sn-stylekit": "1.0.15",
"standard-file-js": "file:~/Desktop/sf/sfjs"
"standard-file-js": "file:~/Desktop/sf/sfjs",
"sn-models": "file:~/Desktop/sn-models",
"connect": "^3.6.6",
"mocha": "^5.2.0",
"serve-static": "^1.13.2",
"chai": "^4.1.2"
},
"license": "GPL-3.0"
}

82
test/mocha/lib/factory.js Normal file
View File

@@ -0,0 +1,82 @@
import '../../../vendor/assets/javascripts/compiled.js';
import '../../../node_modules/chai/chai.js';
import '../vendor/chai-as-promised-built.js';
import '../../../vendor/assets/javascripts/lodash/lodash.custom.js';
import LocalStorageManager from './localStorageManager.js';
const sf_default = new StandardFile();
SFItem.AppDomain = "org.standardnotes.sn";
var _globalStorageManager = null;
var _globalHttpManager = null;
var _globalAuthManager = null;
var _globalModelManager = null;
var _globalStandardFile = null;
export default class Factory {
static initialize() {
this.globalStorageManager();
this.globalHttpManager();
this.globalAuthManager();
this.globalModelManager();
}
static globalStorageManager() {
if(_globalStorageManager == null) { _globalStorageManager = new LocalStorageManager(); }
return _globalStorageManager;
}
static globalHttpManager() {
if(_globalHttpManager == null) { _globalHttpManager = new SFHttpManager(_globalStorageManager); }
return _globalHttpManager;
}
static globalAuthManager() {
if(_globalAuthManager == null) { _globalAuthManager = new SFAuthManager(_globalStorageManager, _globalHttpManager); }
return _globalAuthManager;
}
static globalModelManager() {
if(_globalModelManager == null) { _globalModelManager = new SFModelManager(); }
return _globalModelManager;
}
static globalStandardFile() {
if(_globalStandardFile == null) { _globalStandardFile = new StandardFile(); }
return _globalStandardFile;
}
static createModelManager() {
return new SFModelManager();
}
static createItemParams() {
var params = {
uuid: SFJS.crypto.generateUUIDSync(),
content_type: "Note",
content: {
title: "hello",
text: "world"
}
};
return params;
}
static createItem() {
return new SFItem(this.createItemParams());
}
static serverURL() {
return "http://localhost:3000";
}
static async newRegisteredUser(email, password) {
let url = this.serverURL();
if(!email) email = sf_default.crypto.generateUUIDSync();
if(!password) password = sf_default.crypto.generateUUIDSync();
return this.globalAuthManager().register(url, email, password, false);
}
}
Factory.initialize();

View File

@@ -0,0 +1,69 @@
// A test StorageManager class using LocalStorage
export default class LocalStorageManager extends SFStorageManager {
/* Simple Key/Value Storage */
async setItem(key, value, vaultKey) {
localStorage.setItem(key, value);
}
async getItem(key, vault) {
return localStorage.getItem(key)
}
async removeItem(key, vault) {
localStorage.removeItem(key);
}
async clear() {
// clear only simple key/values
localStorage.clear();
}
/*
Model Storage
*/
async getAllModels() {
var models = [];
for(var key in localStorage) {
if(key.startsWith("item-")) {
models.push(JSON.parse(localStorage[key]))
}
}
return models;
}
async saveModel(item) {
return this.saveModels([item]);
}
async saveModels(items) {
return Promise.all(items.map((item) => {
return this.setItem(`item-${item.uuid}`, JSON.stringify(item));
}))
}
async deleteModel(item,) {
return this.removeItem(`item-${item.uuid}`);
}
async clearAllModels() {
// clear only models
for(var key in localStorage) {
if(key.startsWith("item-")) {
this.removeItem(`item-${item.uuid}`);
}
}
}
/* General */
clearAllData() {
return Promise.all([
this.clear(),
this.clearAllModels()
])
}
}

View File

@@ -1,3 +1,12 @@
import '../../vendor/assets/javascripts/compiled.js';
import '../../node_modules/chai/chai.js';
import './vendor/chai-as-promised-built.js';
import '../../vendor/assets/javascripts/lodash/lodash.custom.js';
import Factory from './lib/factory.js';
chai.use(chaiAsPromised);
var expect = chai.expect;
describe("notes and tags", () => {
const getNoteParams = () => {
var params = {
@@ -27,18 +36,13 @@ describe("notes and tags", () => {
}
]
tagParams.content.references = [
{
uuid: noteParams.uuid,
content_type: noteParams.content_type
}
]
tagParams.content.references = []
return [noteParams, tagParams];
}
it('uses proper class for note', () => {
let modelManager = createModelManager();
let modelManager = Factory.createModelManager();
let noteParams = getNoteParams();
modelManager.mapResponseItemsToLocalModels([noteParams]);
let note = modelManager.allItemsMatchingTypes(["Note"])[0];
@@ -46,14 +50,14 @@ describe("notes and tags", () => {
});
it('creates two-way relationship between note and tag', () => {
let modelManager = createModelManager();
let modelManager = Factory.createModelManager();
let pair = createRelatedNoteTagPair();
let noteParams = pair[0];
let tagParams = pair[1];
expect(tagParams.content.references.length).to.equal(1);
expect(tagParams.content.references.length).to.equal(1);
expect(noteParams.content.references.length).to.equal(1);
expect(tagParams.content.references.length).to.equal(0);
modelManager.mapResponseItemsToLocalModels([noteParams, tagParams]);
let note = modelManager.allItemsMatchingTypes(["Note"])[0];
@@ -67,10 +71,10 @@ describe("notes and tags", () => {
expect(tag).to.not.be.null;
expect(note.content.references.length).to.equal(1);
expect(tag.content.references.length).to.equal(1);
expect(tag.content.references.length).to.equal(0);
expect(note.hasRelationshipWithItem(tag)).to.equal(true);
expect(tag.hasRelationshipWithItem(note)).to.equal(true);
expect(tag.hasRelationshipWithItem(note)).to.equal(false);
expect(note.tags.length).to.equal(1);
expect(tag.notes.length).to.equal(1);
@@ -81,11 +85,13 @@ describe("notes and tags", () => {
// expect to be true
expect(note.dirty).to.be.ok;
expect(tag.dirty).to.be.ok;
// expect to be false
expect(tag.dirty).to.not.be.ok;
});
it('handles remote deletion of relationship', () => {
let modelManager = createModelManager();
let modelManager = Factory.createModelManager();
let pair = createRelatedNoteTagPair();
let noteParams = pair[0];
@@ -96,7 +102,7 @@ describe("notes and tags", () => {
let tag = modelManager.allItemsMatchingTypes(["Tag"])[0];
expect(note.content.references.length).to.equal(1);
expect(tag.content.references.length).to.equal(1);
expect(tag.content.references.length).to.equal(0);
noteParams.content.references = [];
modelManager.mapResponseItemsToLocalModels([noteParams]);
@@ -111,7 +117,7 @@ describe("notes and tags", () => {
});
it('properly handles duplication', () => {
let modelManager = createModelManager();
let modelManager = Factory.createModelManager();
let pair = createRelatedNoteTagPair();
let noteParams = pair[0];
@@ -121,17 +127,55 @@ describe("notes and tags", () => {
let note = modelManager.allItemsMatchingTypes(["Note"])[0];
let tag = modelManager.allItemsMatchingTypes(["Tag"])[0];
var duplicateNote = modelManager.createDuplicateItem(note);
expect(note.uuid).to.equal(duplicateNote.uuid);
// Usually content_type will be provided by a server response
var duplicateParams = _.merge({content_type: "Note"}, note);
duplicateParams.uuid = null;
expect(duplicateParams.content_type).to.equal("Note");
var duplicateNote = modelManager.createDuplicateItem(duplicateParams);
modelManager.addItem(duplicateNote);
expect(note.uuid).to.not.equal(duplicateNote.uuid);
expect(note.content.references.length).to.equal(1);
expect(note.tags.length).to.equal(1);
expect(duplicateNote.content.references.length).to.equal(1);
expect(duplicateNote.tags.length).to.equal(1);
expect(tag.content.references.length).to.equal(1);
expect(tag.notes.length).to.equal(1);
expect(tag.content.references.length).to.equal(0);
expect(tag.notes.length).to.equal(2);
var tagNote1 = tag.notes[0];
var tagNote2 = tag.notes[1];
expect(tagNote1.uuid).to.not.equal(tagNote2.uuid);
// expect to be false
expect(note.dirty).to.not.be.ok;
expect(tag.dirty).to.not.be.ok;
});
it('deleting a tag should update note references', () => {
let modelManager = Factory.createModelManager();
let pair = createRelatedNoteTagPair();
let noteParams = pair[0];
let tagParams = pair[1];
modelManager.mapResponseItemsToLocalModels([noteParams, tagParams]);
let note = modelManager.allItemsMatchingTypes(["Note"])[0];
let tag = modelManager.allItemsMatchingTypes(["Tag"])[0];
expect(note.content.references.length).to.equal(1);
expect(note.tags.length).to.equal(1);
expect(tag.content.references.length).to.equal(0);
expect(tag.notes.length).to.equal(1);
modelManager.setItemToBeDeleted(tag);
modelManager.mapResponseItemsToLocalModels([tag]);
// expect(tag.notes.length).to.equal(0);
expect(note.content.references.length).to.equal(0);
expect(note.tags.length).to.equal(0);
});
});

23
test/mocha/test.html Normal file
View File

@@ -0,0 +1,23 @@
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
<style>
body {
/* background-color: rgb(195, 195, 195); */
}
</style>
</head>
<body>
<div id="mocha"></div>
<script src="../../node_modules/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script type="module" src="models.test.js"></script>
<script type="module">
mocha.checkLeaks();
mocha.run();
</script>
</body>
</html>

View File

@@ -0,0 +1,539 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.chaiAsPromised = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
/* eslint-disable no-invalid-this */
let checkError = require("check-error");
module.exports = (chai, utils) => {
const Assertion = chai.Assertion;
const assert = chai.assert;
const proxify = utils.proxify;
// If we are using a version of Chai that has checkError on it,
// we want to use that version to be consistent. Otherwise, we use
// what was passed to the factory.
if (utils.checkError) {
checkError = utils.checkError;
}
function isLegacyJQueryPromise(thenable) {
// jQuery promises are Promises/A+-compatible since 3.0.0. jQuery 3.0.0 is also the first version
// to define the catch method.
return typeof thenable.catch !== "function" &&
typeof thenable.always === "function" &&
typeof thenable.done === "function" &&
typeof thenable.fail === "function" &&
typeof thenable.pipe === "function" &&
typeof thenable.progress === "function" &&
typeof thenable.state === "function";
}
function assertIsAboutPromise(assertion) {
if (typeof assertion._obj.then !== "function") {
throw new TypeError(utils.inspect(assertion._obj) + " is not a thenable.");
}
if (isLegacyJQueryPromise(assertion._obj)) {
throw new TypeError("Chai as Promised is incompatible with thenables of jQuery<3.0.0, sorry! Please " +
"upgrade jQuery or use another Promises/A+ compatible library (see " +
"http://promisesaplus.com/).");
}
}
function proxifyIfSupported(assertion) {
return proxify === undefined ? assertion : proxify(assertion);
}
function method(name, asserter) {
utils.addMethod(Assertion.prototype, name, function () {
assertIsAboutPromise(this);
return asserter.apply(this, arguments);
});
}
function property(name, asserter) {
utils.addProperty(Assertion.prototype, name, function () {
assertIsAboutPromise(this);
return proxifyIfSupported(asserter.apply(this, arguments));
});
}
function doNotify(promise, done) {
promise.then(() => done(), done);
}
// These are for clarity and to bypass Chai refusing to allow `undefined` as actual when used with `assert`.
function assertIfNegated(assertion, message, extra) {
assertion.assert(true, null, message, extra.expected, extra.actual);
}
function assertIfNotNegated(assertion, message, extra) {
assertion.assert(false, message, null, extra.expected, extra.actual);
}
function getBasePromise(assertion) {
// We need to chain subsequent asserters on top of ones in the chain already (consider
// `eventually.have.property("foo").that.equals("bar")`), only running them after the existing ones pass.
// So the first base-promise is `assertion._obj`, but after that we use the assertions themselves, i.e.
// previously derived promises, to chain off of.
return typeof assertion.then === "function" ? assertion : assertion._obj;
}
function getReasonName(reason) {
return reason instanceof Error ? reason.toString() : checkError.getConstructorName(reason);
}
// Grab these first, before we modify `Assertion.prototype`.
const propertyNames = Object.getOwnPropertyNames(Assertion.prototype);
const propertyDescs = {};
for (const name of propertyNames) {
propertyDescs[name] = Object.getOwnPropertyDescriptor(Assertion.prototype, name);
}
property("fulfilled", function () {
const derivedPromise = getBasePromise(this).then(
value => {
assertIfNegated(this,
"expected promise not to be fulfilled but it was fulfilled with #{act}",
{ actual: value });
return value;
},
reason => {
assertIfNotNegated(this,
"expected promise to be fulfilled but it was rejected with #{act}",
{ actual: getReasonName(reason) });
return reason;
}
);
module.exports.transferPromiseness(this, derivedPromise);
return this;
});
property("rejected", function () {
const derivedPromise = getBasePromise(this).then(
value => {
assertIfNotNegated(this,
"expected promise to be rejected but it was fulfilled with #{act}",
{ actual: value });
return value;
},
reason => {
assertIfNegated(this,
"expected promise not to be rejected but it was rejected with #{act}",
{ actual: getReasonName(reason) });
// Return the reason, transforming this into a fulfillment, to allow further assertions, e.g.
// `promise.should.be.rejected.and.eventually.equal("reason")`.
return reason;
}
);
module.exports.transferPromiseness(this, derivedPromise);
return this;
});
method("rejectedWith", function (errorLike, errMsgMatcher, message) {
let errorLikeName = null;
const negate = utils.flag(this, "negate") || false;
// rejectedWith with that is called without arguments is
// the same as a plain ".rejected" use.
if (errorLike === undefined && errMsgMatcher === undefined &&
message === undefined) {
/* eslint-disable no-unused-expressions */
return this.rejected;
/* eslint-enable no-unused-expressions */
}
if (message !== undefined) {
utils.flag(this, "message", message);
}
if (errorLike instanceof RegExp || typeof errorLike === "string") {
errMsgMatcher = errorLike;
errorLike = null;
} else if (errorLike && errorLike instanceof Error) {
errorLikeName = errorLike.toString();
} else if (typeof errorLike === "function") {
errorLikeName = checkError.getConstructorName(errorLike);
} else {
errorLike = null;
}
const everyArgIsDefined = Boolean(errorLike && errMsgMatcher);
let matcherRelation = "including";
if (errMsgMatcher instanceof RegExp) {
matcherRelation = "matching";
}
const derivedPromise = getBasePromise(this).then(
value => {
let assertionMessage = null;
let expected = null;
if (errorLike) {
assertionMessage = "expected promise to be rejected with #{exp} but it was fulfilled with #{act}";
expected = errorLikeName;
} else if (errMsgMatcher) {
assertionMessage = `expected promise to be rejected with an error ${matcherRelation} #{exp} but ` +
`it was fulfilled with #{act}`;
expected = errMsgMatcher;
}
assertIfNotNegated(this, assertionMessage, { expected, actual: value });
return value;
},
reason => {
const errorLikeCompatible = errorLike && (errorLike instanceof Error ?
checkError.compatibleInstance(reason, errorLike) :
checkError.compatibleConstructor(reason, errorLike));
const errMsgMatcherCompatible = errMsgMatcher && checkError.compatibleMessage(reason, errMsgMatcher);
const reasonName = getReasonName(reason);
if (negate && everyArgIsDefined) {
if (errorLikeCompatible && errMsgMatcherCompatible) {
this.assert(true,
null,
"expected promise not to be rejected with #{exp} but it was rejected " +
"with #{act}",
errorLikeName,
reasonName);
}
} else {
if (errorLike) {
this.assert(errorLikeCompatible,
"expected promise to be rejected with #{exp} but it was rejected with #{act}",
"expected promise not to be rejected with #{exp} but it was rejected " +
"with #{act}",
errorLikeName,
reasonName);
}
if (errMsgMatcher) {
this.assert(errMsgMatcherCompatible,
`expected promise to be rejected with an error ${matcherRelation} #{exp} but got ` +
`#{act}`,
`expected promise not to be rejected with an error ${matcherRelation} #{exp}`,
errMsgMatcher,
checkError.getMessage(reason));
}
}
return reason;
}
);
module.exports.transferPromiseness(this, derivedPromise);
return this;
});
property("eventually", function () {
utils.flag(this, "eventually", true);
return this;
});
method("notify", function (done) {
doNotify(getBasePromise(this), done);
return this;
});
method("become", function (value, message) {
return this.eventually.deep.equal(value, message);
});
// ### `eventually`
// We need to be careful not to trigger any getters, thus `Object.getOwnPropertyDescriptor` usage.
const methodNames = propertyNames.filter(name => {
return name !== "assert" && typeof propertyDescs[name].value === "function";
});
methodNames.forEach(methodName => {
Assertion.overwriteMethod(methodName, originalMethod => function () {
return doAsserterAsyncAndAddThen(originalMethod, this, arguments);
});
});
const getterNames = propertyNames.filter(name => {
return name !== "_obj" && typeof propertyDescs[name].get === "function";
});
getterNames.forEach(getterName => {
// Chainable methods are things like `an`, which can work both for `.should.be.an.instanceOf` and as
// `should.be.an("object")`. We need to handle those specially.
const isChainableMethod = Assertion.prototype.__methods.hasOwnProperty(getterName);
if (isChainableMethod) {
Assertion.overwriteChainableMethod(
getterName,
originalMethod => function () {
return doAsserterAsyncAndAddThen(originalMethod, this, arguments);
},
originalGetter => function () {
return doAsserterAsyncAndAddThen(originalGetter, this);
}
);
} else {
Assertion.overwriteProperty(getterName, originalGetter => function () {
return proxifyIfSupported(doAsserterAsyncAndAddThen(originalGetter, this));
});
}
});
function doAsserterAsyncAndAddThen(asserter, assertion, args) {
// Since we're intercepting all methods/properties, we need to just pass through if they don't want
// `eventually`, or if we've already fulfilled the promise (see below).
if (!utils.flag(assertion, "eventually")) {
asserter.apply(assertion, args);
return assertion;
}
const derivedPromise = getBasePromise(assertion).then(value => {
// Set up the environment for the asserter to actually run: `_obj` should be the fulfillment value, and
// now that we have the value, we're no longer in "eventually" mode, so we won't run any of this code,
// just the base Chai code that we get to via the short-circuit above.
assertion._obj = value;
utils.flag(assertion, "eventually", false);
return args ? module.exports.transformAsserterArgs(args) : args;
}).then(newArgs => {
asserter.apply(assertion, newArgs);
// Because asserters, for example `property`, can change the value of `_obj` (i.e. change the "object"
// flag), we need to communicate this value change to subsequent chained asserters. Since we build a
// promise chain paralleling the asserter chain, we can use it to communicate such changes.
return assertion._obj;
});
module.exports.transferPromiseness(assertion, derivedPromise);
return assertion;
}
// ### Now use the `Assertion` framework to build an `assert` interface.
const originalAssertMethods = Object.getOwnPropertyNames(assert).filter(propName => {
return typeof assert[propName] === "function";
});
assert.isFulfilled = (promise, message) => (new Assertion(promise, message)).to.be.fulfilled;
assert.isRejected = (promise, errorLike, errMsgMatcher, message) => {
const assertion = new Assertion(promise, message);
return assertion.to.be.rejectedWith(errorLike, errMsgMatcher, message);
};
assert.becomes = (promise, value, message) => assert.eventually.deepEqual(promise, value, message);
assert.doesNotBecome = (promise, value, message) => assert.eventually.notDeepEqual(promise, value, message);
assert.eventually = {};
originalAssertMethods.forEach(assertMethodName => {
assert.eventually[assertMethodName] = function (promise) {
const otherArgs = Array.prototype.slice.call(arguments, 1);
let customRejectionHandler;
const message = arguments[assert[assertMethodName].length - 1];
if (typeof message === "string") {
customRejectionHandler = reason => {
throw new chai.AssertionError(`${message}\n\nOriginal reason: ${utils.inspect(reason)}`);
};
}
const returnedPromise = promise.then(
fulfillmentValue => assert[assertMethodName].apply(assert, [fulfillmentValue].concat(otherArgs)),
customRejectionHandler
);
returnedPromise.notify = done => {
doNotify(returnedPromise, done);
};
return returnedPromise;
};
});
};
module.exports.transferPromiseness = (assertion, promise) => {
assertion.then = promise.then.bind(promise);
};
module.exports.transformAsserterArgs = values => values;
},{"check-error":2}],2:[function(require,module,exports){
'use strict';
/* !
* Chai - checkError utility
* Copyright(c) 2012-2016 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/
/**
* ### .checkError
*
* Checks that an error conforms to a given set of criteria and/or retrieves information about it.
*
* @api public
*/
/**
* ### .compatibleInstance(thrown, errorLike)
*
* Checks if two instances are compatible (strict equal).
* Returns false if errorLike is not an instance of Error, because instances
* can only be compatible if they're both error instances.
*
* @name compatibleInstance
* @param {Error} thrown error
* @param {Error|ErrorConstructor} errorLike object to compare against
* @namespace Utils
* @api public
*/
function compatibleInstance(thrown, errorLike) {
return errorLike instanceof Error && thrown === errorLike;
}
/**
* ### .compatibleConstructor(thrown, errorLike)
*
* Checks if two constructors are compatible.
* This function can receive either an error constructor or
* an error instance as the `errorLike` argument.
* Constructors are compatible if they're the same or if one is
* an instance of another.
*
* @name compatibleConstructor
* @param {Error} thrown error
* @param {Error|ErrorConstructor} errorLike object to compare against
* @namespace Utils
* @api public
*/
function compatibleConstructor(thrown, errorLike) {
if (errorLike instanceof Error) {
// If `errorLike` is an instance of any error we compare their constructors
return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
} else if (errorLike.prototype instanceof Error || errorLike === Error) {
// If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
return thrown.constructor === errorLike || thrown instanceof errorLike;
}
return false;
}
/**
* ### .compatibleMessage(thrown, errMatcher)
*
* Checks if an error's message is compatible with a matcher (String or RegExp).
* If the message contains the String or passes the RegExp test,
* it is considered compatible.
*
* @name compatibleMessage
* @param {Error} thrown error
* @param {String|RegExp} errMatcher to look for into the message
* @namespace Utils
* @api public
*/
function compatibleMessage(thrown, errMatcher) {
var comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
if (errMatcher instanceof RegExp) {
return errMatcher.test(comparisonString);
} else if (typeof errMatcher === 'string') {
return comparisonString.indexOf(errMatcher) !== -1; // eslint-disable-line no-magic-numbers
}
return false;
}
/**
* ### .getFunctionName(constructorFn)
*
* Returns the name of a function.
* This also includes a polyfill function if `constructorFn.name` is not defined.
*
* @name getFunctionName
* @param {Function} constructorFn
* @namespace Utils
* @api private
*/
var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\(\/]+)/;
function getFunctionName(constructorFn) {
var name = '';
if (typeof constructorFn.name === 'undefined') {
// Here we run a polyfill if constructorFn.name is not defined
var match = String(constructorFn).match(functionNameMatch);
if (match) {
name = match[1];
}
} else {
name = constructorFn.name;
}
return name;
}
/**
* ### .getConstructorName(errorLike)
*
* Gets the constructor name for an Error instance or constructor itself.
*
* @name getConstructorName
* @param {Error|ErrorConstructor} errorLike
* @namespace Utils
* @api public
*/
function getConstructorName(errorLike) {
var constructorName = errorLike;
if (errorLike instanceof Error) {
constructorName = getFunctionName(errorLike.constructor);
} else if (typeof errorLike === 'function') {
// If `err` is not an instance of Error it is an error constructor itself or another function.
// If we've got a common function we get its name, otherwise we may need to create a new instance
// of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more.
constructorName = getFunctionName(errorLike).trim() ||
getFunctionName(new errorLike()); // eslint-disable-line new-cap
}
return constructorName;
}
/**
* ### .getMessage(errorLike)
*
* Gets the error message from an error.
* If `err` is a String itself, we return it.
* If the error has no message, we return an empty string.
*
* @name getMessage
* @param {Error|String} errorLike
* @namespace Utils
* @api public
*/
function getMessage(errorLike) {
var msg = '';
if (errorLike && errorLike.message) {
msg = errorLike.message;
} else if (typeof errorLike === 'string') {
msg = errorLike;
}
return msg;
}
module.exports = {
compatibleInstance: compatibleInstance,
compatibleConstructor: compatibleConstructor,
compatibleMessage: compatibleMessage,
getMessage: getMessage,
getConstructorName: getConstructorName,
};
},{}]},{},[1])(1)
});

7
testing-server.js Normal file
View File

@@ -0,0 +1,7 @@
// Used for running mocha tests
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(8081, function(){
console.log('Server running on 8081...');
});