Skip to content

Commit

Permalink
MemServer server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jan 31, 2020
1 parent 1f4847d commit 707cd8b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 42 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@ava/typescript": "^1.0.0",
"@types/fs-extra": "^8.0.1",
"@types/jquery": "^3.3.31",
"@types/node": "^13.5.3",
"ava": "3.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand All @@ -45,9 +46,13 @@
"typescript": "^3.7.5"
},
"ava": {
"files": [
"src/test/**/*"
],
"extensions": [
"ts"
],
"cache": false,
"require": [
"ts-node/register"
],
Expand Down
30 changes: 14 additions & 16 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,20 @@ async function createFixtureAndModelFoldersIfNeeded(memServerDirectory) {
}

async function openConsole() {
if (process.cwd().includes("memserver")) {
throw new Error(
chalk.red(
"[MemServer CLI] You are in the memserver directory, go to the root of your project to start memserver console."
)
);
}

const MemServer = (await import("./index")).default;
const repl = (await import("repl")).default;

console.log(
chalk.cyan("[MemServer CLI]"),
"Starting MemServer node.js console - Remember to MemServer.start() ;)"
);
repl.start("> ");
// if (process.cwd().includes("memserver")) {
// throw new Error(
// chalk.red(
// "[MemServer CLI] You are in the memserver directory, go to the root of your project to start memserver console."
// )
// );
// }
// const MemServer = (await import("./index")).default;
// const repl = (await import("repl")).default;
// console.log(
// chalk.cyan("[MemServer CLI]"),
// "Starting MemServer node.js console - Remember to MemServer.start() ;)"
// );
// repl.start("> ");
}

async function getMemServerDirectory() {
Expand Down
18 changes: 11 additions & 7 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import util from "util";
import chalk from "ansi-colors";
import Inflector from "i";
import emberCliStringUtils from "ember-cli-string-utils";
import { classify, underscore } from "ember-cli-string-utils";
import { primaryKeyTypeSafetyCheck, generateUUID } from "./utils";

const { classify, underscore } = emberCliStringUtils;
const { singularize, pluralize } = Inflector();

interface InternalMemServerModel {
type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
}[Keys];

interface PrimaryKeys {
id?: number;
uuid?: string;
}

type returnValue = InternalMemServerModel | Array<InternalMemServerModel> | undefined;
type InternalMemServerModel = RequireOnlyOne<PrimaryKeys, "id" | "uuid">;

// NOTE: probably needs .reset() method;
export default abstract class MemServerModel {
Expand Down Expand Up @@ -110,8 +114,8 @@ export default abstract class MemServerModel {

return this.DB.find((model) => comparison(model, options, keys, 0));
}
static findAll(options = {}): Array<object> {
const models: Array<object> = Array.from(this.DB);
static findAll(options = {}): Array<InternalMemServerModel> {
const models: Array<InternalMemServerModel> = Array.from(this.DB);
const keys = Object.keys(options);

if (keys.length === 0) {
Expand All @@ -120,7 +124,7 @@ export default abstract class MemServerModel {

return models.filter((model) => comparison(model, options, keys, 0));
}
static insert(options: InternalMemServerModel | undefined): object {
static insert(options: InternalMemServerModel | undefined): InternalMemServerModel {
const models = this.DB;

if (models.length === 0) {
Expand Down
12 changes: 10 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
declare global {
interface Window {
Pretender: any;
RouteRecognizer: any;
FakeXMLHttpRequest: any;
}
}

import chalk from "ansi-colors";
import { primaryKeyTypeSafetyCheck } from "./utils";
import FakeXMLHttpRequest from "fake-xml-http-request";
Expand All @@ -15,7 +23,7 @@ window.RouteRecognizer = RouteRecognizer;

// MemServer.{"Server", "shutdown"}
export default class MemServer {
constructor(options = {}) {
constructor(options = { logging: true, initializer: () => {}, routes: () => {} }) {
const initializer = options.initializer || function() {}; // fixtures could be loaded here
const routes = options.routes || function() {};
const logging = options.hasOwnProperty("logging") ? options.logging : true;
Expand Down Expand Up @@ -53,7 +61,7 @@ function startPretender(routes, options) {
}

this.unhandledRequest = function(verb, path, request) {
console.log(MemServer, chalk.red("[UNHANDLED REQUEST]", verb, path));
console.log(MemServer, chalk.red("[UNHANDLED REQUEST]"), verb, path);
console.log(chalk.red("UNHANDLED REQUEST WAS:\n"), request);
console.log(request);
};
Expand Down
15 changes: 15 additions & 0 deletions src/setup-dom.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
declare global {
interface Window {
$?: any;
}

namespace NodeJS {
interface Global {
window?: any;
document?: any;
self: any;
$?: any;
}
}
}

export default async function() {
const { JSDOM } = (await import("jsdom")).default;

Expand Down
14 changes: 6 additions & 8 deletions src/test/server/config.ts → src/test/server/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test.before(async () => {
return 'Some default name';
}
}
}
`
),
Expand Down Expand Up @@ -125,18 +126,15 @@ test.serial(
import Photo from '${CWD}/memserver/models/photo';
import Response from '../dist/response';
// console.log('Photo is', Photo);
export default function() {
this.get('/photos', () => {
// const photos = Photo.findAll();
const photos = Photo.findAll();
// if (!photos || photos.length === 0) {
// return Response(404, { error: 'Not found' });
// }
if (!photos || photos.length === 0) {
return Response(404, { error: 'Not found' });
}
// return { photos: Photo.serializer(photos) };
return { photos: [] };
return { photos: Photo.serializer(photos) };
});
}
`
Expand Down
13 changes: 7 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"compilerOptions": {
/* Basic Options */
"incremental": true, /* Enable incremental compilation */
"incremental": false, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
//"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
//"sourceMap": true, /* Generates corresponding '.map' file. */
//"outFile": "./index.js", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
//"rootDir": "./src/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
Expand Down Expand Up @@ -65,6 +65,7 @@
},
"exclude": [
"memserver-boilerplate",
"memserver"
"memserver",
"dist"
]
}

0 comments on commit 707cd8b

Please sign in to comment.