Skip to content

Commit

Permalink
Merge pull request #52 from kenneth-gray/scenarios-endpoint
Browse files Browse the repository at this point in the history
feat: added new scenarios endpoint
  • Loading branch information
kenneth-gray authored Feb 5, 2022
2 parents 423e702 + 3d96be0 commit 2b77607
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 64 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Returns an http server, with an additional kill method

#### options

> `{ port, uiPath, modifyScenariosPath, resetScenariosPath }` | defaults to `{}`
> `{ port, uiPath, modifyScenariosPath, resetScenariosPath, scenariosPath }` | defaults to `{}`
<!-- https://www.tablesgenerator.com/markdown_tables -->

Expand All @@ -110,6 +110,7 @@ Returns an http server, with an additional kill method
| uiPath | `string` | `/` | Path that the UI will load on. `http://localhost:{port}{uiPath}` |
| modifyScenariosPath | `string` | `/modify-scenarios` | API path for modifying scenarios. `http://localhost:{port}{modifyScenariosPath}` |
| resetScenariosPath | `string` | `/reset-scenarios` | API path for resetting scenarios. `http://localhost:{port}{resetScenariosPath}` |
| scenariosPath | `string` | `/scenarios` | API path for getting scenarios. `http://localhost:{port}{scenariosPath}` |
| cookieMode | `boolean` | `false` | Whether or not to store scenario selections in a cookie rather than directly in the server |

## Types
Expand Down
2 changes: 1 addition & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ run({
},
options: {
port: 5000,
uiPath: '/scenarios',
uiPath: '/scenarios-ui',
modifyScenariosPath: '/modify',
resetScenariosPath: '/reset',
},
Expand Down
4 changes: 2 additions & 2 deletions src/Html.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { Groups } from './types';
import { UiGroups } from './types';

function Html({
updatedScenarios,
Expand All @@ -9,7 +9,7 @@ function Html({
other,
}: {
uiPath: string;
groups: Groups;
groups: UiGroups;
other: Array<{ name: string; checked: boolean }>;
updatedScenarios?: string[];
}) {
Expand Down
17 changes: 16 additions & 1 deletion src/apis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RequestHandler, Response, Request } from 'express';
import { ScenarioMap } from './types';
import { getScenarios as utilsGetScenarios } from './utils/get-scenarios';

export { modifyScenarios, resetScenarios };
export { modifyScenarios, resetScenarios, getScenarios };

function modifyScenarios({
scenarioNames,
Expand Down Expand Up @@ -60,3 +61,17 @@ function resetScenarios({
res.sendStatus(204);
};
}

function getScenarios({
scenarioMap,
getScenarioNames,
}: {
scenarioMap: ScenarioMap;
getScenarioNames: (req: Request, res: Response) => string[];
}): RequestHandler {
return (req: Request, res: Response) => {
const data = utilsGetScenarios(scenarioMap, getScenarioNames(req, res));

res.json(data);
};
}
149 changes: 148 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ describe('run', () => {
});
});

it('reset-scenarios and modify-scenarios paths can be changed', async () => {
it('reset-scenarios, modify-scenarios and scenarios paths can be changed', async () => {
const initialResponse = { something: 'old' };
const scenarioResponse = { something: 'new' };
const server = run({
Expand All @@ -1336,10 +1336,24 @@ describe('run', () => {
options: {
modifyScenariosPath: '/modify',
resetScenariosPath: '/reset',
scenariosPath: '/get-scenarios',
},
});

await serverTest(server, async () => {
const scenariosResponse = await rp.get(
'http://localhost:3000/get-scenarios',
{
json: true,
},
);
expect(scenariosResponse).toEqual(
expect.objectContaining({
groups: expect.anything(),
other: expect.anything(),
}),
);

const firstResponse = await rp.get('http://localhost:3000/test-me', {
json: true,
});
Expand Down Expand Up @@ -1454,6 +1468,139 @@ describe('run', () => {
});
});
});

describe('GET scenarios', () => {
it('returns grouped and other scenarios', async () => {
const server = run({
default: [],
scenarios: {
test1: [
{
url: '/test-me-1',
method: 'GET',
},
],
test2: [
{
url: '/test-me-2',
method: 'GET',
},
],
test3: {
group: 'abc',
mocks: [
{
url: '/test-me-3',
method: 'GET',
},
],
},
test4: {
group: 'abc',
mocks: [
{
url: '/test-me-4',
method: 'GET',
},
],
},
},
});

await serverTest(server, async () => {
const scenariosResponse = await rp.get(
'http://localhost:3000/scenarios',
{
json: true,
},
);

expect(scenariosResponse).toEqual({
groups: [
{
name: 'abc',
scenarios: [
{ id: 'test3', selected: false },
{ id: 'test4', selected: false },
],
},
],
other: [
{ id: 'test1', selected: false },
{ id: 'test2', selected: false },
],
});
});
});

it('returns the correct value for "selected"', async () => {
const server = run({
default: [],
scenarios: {
test1: [
{
url: '/test-me-1',
method: 'GET',
},
],
test2: [
{
url: '/test-me-2',
method: 'GET',
},
],
test3: {
group: 'abc',
mocks: [
{
url: '/test-me-3',
method: 'GET',
},
],
},
test4: {
group: 'abc',
mocks: [
{
url: '/test-me-4',
method: 'GET',
},
],
},
},
});

await serverTest(server, async () => {
await rp.put('http://localhost:3000/modify-scenarios', {
body: { scenarios: ['test2', 'test3'] },
json: true,
});

const scenariosResponse = await rp.get(
'http://localhost:3000/scenarios',
{
json: true,
},
);

expect(scenariosResponse).toEqual({
groups: [
{
name: 'abc',
scenarios: [
{ id: 'test3', selected: true },
{ id: 'test4', selected: false },
],
},
],
other: [
{ id: 'test1', selected: false },
{ id: 'test2', selected: true },
],
});
});
});
});
});

function getStartTime() {
Expand Down
15 changes: 14 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import express, { Request, Response, NextFunction } from 'express';
import path from 'path';
import { transform } from 'server-with-kill';

import { modifyScenarios, resetScenarios } from './apis';
import {
modifyScenarios,
resetScenarios,
getScenarios as apiGetScenarios,
} from './apis';
import {
getGraphQlMocks,
getGraphQlMock,
Expand Down Expand Up @@ -70,6 +74,7 @@ function createExpressApp({
uiPath = '/',
modifyScenariosPath = '/modify-scenarios',
resetScenariosPath = '/reset-scenarios',
scenariosPath = '/scenarios',
cookieMode = false,
} = options;

Expand Down Expand Up @@ -134,6 +139,14 @@ function createExpressApp({
}),
);

app.get(
scenariosPath,
apiGetScenarios({
scenarioMap,
getScenarioNames,
}),
);

app.use(
createRequestHandler({
getScenarioNames,
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type Options = {
uiPath?: string;
modifyScenariosPath?: string;
resetScenariosPath?: string;
scenariosPath?: string;
cookieMode?: boolean;
};

Expand All @@ -97,7 +98,7 @@ export type UpdateContext = (partialContext: PartialContext) => Context;

export type GetContext = () => Context;

export type Groups = Array<{
export type UiGroups = Array<{
name: string;
noneChecked: boolean;
scenarios: Array<{
Expand All @@ -106,6 +107,14 @@ export type Groups = Array<{
}>;
}>;

export type Groups = Array<{
name: string;
scenarios: Array<{
id: string;
selected: boolean;
}>;
}>;

export type CookieValue = {
context: Context;
scenarios: string[];
Expand Down
Loading

0 comments on commit 2b77607

Please sign in to comment.