Skip to content

Commit

Permalink
Server tests complete
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Feb 3, 2020
1 parent 6197a4c commit 1b2d7bf
Show file tree
Hide file tree
Showing 5 changed files with 463 additions and 10 deletions.
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const CWD = process.cwd();

await setupDom();

const modelFileNames = await fs.readdir(`${CWD}/memserver/models`);

window.Memserver = (await import("./server")).default;

const [initializerModule, routesModule] = await Promise.all([
Expand Down
4 changes: 2 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type InternalModel = RequireOnlyOne<InternalModelShape, "id" | "uuid">;

export default abstract class MemServerModel {
static _DB = {};
static _modelDefinitions = [];
static _modelDefinitions = {};
static _attributes = {};
static _defaultAttributes = {}; // NOTE: probably a decorator here in future
static _embedReferences = {}; // NOTE: serializer concern
Expand All @@ -40,7 +40,7 @@ export default abstract class MemServerModel {
static get attributes(): Array<string> {
if (!this._attributes[this.name]) {
this._attributes[this.name] = [];
this._modelDefinitions.push(this);
this._modelDefinitions[this.name] = this;

return this._attributes[this.name];
}
Expand Down
4 changes: 2 additions & 2 deletions src/pretender-hacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import qs from "qs";
import chalk from "ansi-colors";
import Inflector from "i";
import stringUtils from "ember-cli-string-utils";
import Model from "./model";

const { classify } = stringUtils;
const { singularize } = Inflector();
const targetNamespace = typeof global === "object" ? global : window;

// HACK START: Pretender Request Parameter Type Casting Hack: Because types are important.
window.Pretender.prototype._handlerFor = function(verb, url, request) {
Expand Down Expand Up @@ -178,7 +178,7 @@ function getDefaultRouteHandler(verb, path) {
const lastPath = paths[paths.length - 1];
const pluralResourceName = lastPath.includes(":") ? paths[paths.length - 2] : lastPath;
const resourceName = singularize(pluralResourceName);
const ResourceModel = targetNamespace[classify(resourceName)];
const ResourceModel = Model._modelDefinitions[classify(resourceName)];

if (!ResourceModel) {
throw new Error(
Expand Down
8 changes: 4 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export default class Memserver {
if (initializerReturn instanceof Promise) {
initializerReturn.then(() => {
if (options.globalizeModels) {
Model._modelDefinitions.forEach((model) => {
window[model.name] = model;
Object.keys(Model._modelDefinitions).forEach((modelName) => {
window[modelName] = Model._modelDefinitions[modelName];
});
}
});
} else {
if (options.globalizeModels) {
Model._modelDefinitions.forEach((model) => {
window[model.name] = model;
Object.keys(Model._modelDefinitions).forEach((modelName) => {
window[modelName] = Model._modelDefinitions[modelName];
});
}
}
Expand Down
Loading

0 comments on commit 1b2d7bf

Please sign in to comment.