Skip to content

Commit

Permalink
add unit testsfor editing config middleware; remove base middleware i…
Browse files Browse the repository at this point in the history
…nheritance
  • Loading branch information
yavorsk committed Feb 2, 2024
1 parent 1fe1997 commit bf26f8c
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { spy } from 'sinon';
import { expect } from 'chai';
import { EditingConfigMiddleware } from './editing-config-middleware';
import { QUERY_PARAM_EDITING_SECRET } from './constants';

type Query = {
[key: string]: string;
};

const mockRequest = (method: string, query?: Query) => {
return {
method,
query: query ?? {},
} as NextApiRequest;
};

const mockResponse = () => {
const res = {} as NextApiResponse;
res.status = spy(() => {
return res;
});
res.json = spy(() => {
return res;
});
res.end = spy(() => {
return res;
});
return res;
};

const componentsArray = ['TestComponentOne', 'TestComponentTwo'];
const componentsMap = new Map<string, unknown>();
componentsMap.set('TestComponentOne', {});
componentsMap.set('TestComponentTwo', {});
const metadata = { packages: { testPackageOne: '0.1.1' } };

const expectedResult = {
components: ['TestComponentOne', 'TestComponentTwo'],
packages: { testPackageOne: '0.1.1' },
};

describe('EditingConfigMiddleware', () => {
const secret = 'jss-editing-secret-mock';

beforeEach(() => {
process.env.JSS_EDITING_SECRET = secret;
});

after(() => {
delete process.env.JSS_EDITING_SECRET;
});

it('should respond with 401 for missing secret', async () => {
const key = 'wrongkey';
const query = { key } as Query;
const req = mockRequest('GET', query);
const res = mockResponse();

const middleware = new EditingConfigMiddleware({ components: componentsArray, metadata });
const handler = middleware.getHandler();

await handler(req, res);

expect(res.status).to.have.been.calledWith(401);
expect(res.end).to.have.been.calledOnce;
});

it('should respond with 401 for invalid secret', async () => {
const key = 'wrongkey';
const query = { key } as Query;
query[QUERY_PARAM_EDITING_SECRET] = 'wrongsekret';
const req = mockRequest('GET', query);
const res = mockResponse();

const middleware = new EditingConfigMiddleware({ components: componentsArray, metadata });
const handler = middleware.getHandler();

await handler(req, res);

expect(res.status).to.have.been.calledWith(401);
expect(res.end).to.have.been.calledOnce;
});

it('should respond with 200 and return config data with components array as argument', async () => {
const key = 'wrongkey';
const query = { key } as Query;
query[QUERY_PARAM_EDITING_SECRET] = secret;
const req = mockRequest('GET', query);
const res = mockResponse();

const middleware = new EditingConfigMiddleware({ components: componentsArray, metadata });
const handler = middleware.getHandler();

await handler(req, res);

expect(res.status).to.have.been.calledOnce;
expect(res.status).to.have.been.calledWith(200);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith(expectedResult);
});

it('should respond with 200 and return config data with components map as argument', async () => {
const key = 'wrongkey';
const query = { key } as Query;
query[QUERY_PARAM_EDITING_SECRET] = secret;
const req = mockRequest('GET', query);
const res = mockResponse();

const middleware = new EditingConfigMiddleware({ components: componentsMap, metadata });
const handler = middleware.getHandler();

await handler(req, res);

expect(res.status).to.have.been.calledOnce;
expect(res.status).to.have.been.calledWith(200);
expect(res.json).to.have.been.calledOnce;
expect(res.json).to.have.been.calledWith(expectedResult);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { MiddlewareBase, MiddlewareBaseConfig } from '../middleware/middleware';
import { QUERY_PARAM_EDITING_SECRET } from './constants';
import { getJssEditingSecret } from '../utils/utils';
import { debug } from '@sitecore-jss/sitecore-jss';
Expand All @@ -8,7 +7,7 @@ interface Metadata {
packages: { [key: string]: string };
}

export type EditingConfigMiddlewareConfig = MiddlewareBaseConfig & {
export type EditingConfigMiddlewareConfig = {
/**
* Components available in the application
*/
Expand All @@ -19,10 +18,8 @@ export type EditingConfigMiddlewareConfig = MiddlewareBaseConfig & {
metadata: Metadata;
};

export class EditingConfigMiddleware extends MiddlewareBase {
constructor(protected config: EditingConfigMiddlewareConfig) {
super(config);
}
export class EditingConfigMiddleware {
constructor(protected config: EditingConfigMiddlewareConfig) { }

/**
* Gets the Next.js API route handler
Expand All @@ -43,7 +40,7 @@ export class EditingConfigMiddleware extends MiddlewareBase {

res.status(401).end('Missing or invalid editing secret');
}

console.log(this.config.components);
const components = Array.isArray(this.config.components)
? this.config.components
: Array.from(this.config.components.keys());
Expand Down

0 comments on commit bf26f8c

Please sign in to comment.