-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit testsfor editing config middleware; remove base middleware i…
…nheritance
- Loading branch information
Showing
2 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
packages/sitecore-jss-nextjs/src/editing/editing-config-middleware.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters