|
1 | 1 | import * as React from "react"; |
2 | 2 |
|
3 | 3 | import * as rules from "./rules"; |
4 | | -import { default as withSecureHeaders, createHeadersObject } from "./index"; |
| 4 | +import { createHeadersObject, default as withSecureHeaders } from "./index"; |
5 | 5 |
|
6 | 6 | describe("createHeadersObject", () => { |
7 | 7 | let contentSecurityPolicyHeaderCreatorSpy: jest.SpyInstance< |
@@ -188,5 +188,28 @@ describe("withSecureHeaders", () => { |
188 | 188 | await expect(ComponentWithSecureHeaders.getInitialProps!(dummyContext)).resolves.toEqual(dummyInitialProps); |
189 | 189 | }); |
190 | 190 | }); |
| 191 | + |
| 192 | + context('when "headersSent" property of "res" is true', () => { |
| 193 | + it('should not call "context.ctx.res.setHeader()"', async () => { |
| 194 | + const DummyComponent = () => React.createElement("div"); |
| 195 | + const ComponentWithSecureHeaders = withSecureHeaders()(DummyComponent); |
| 196 | + |
| 197 | + const resSetHeeaderSpy = jest.fn(); |
| 198 | + const dummyContext: any = { ctx: { res: { setHeader: resSetHeeaderSpy, headersSent: true } } }; |
| 199 | + await ComponentWithSecureHeaders.getInitialProps!(dummyContext); |
| 200 | + |
| 201 | + expect(resSetHeeaderSpy).not.toBeCalled(); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should return the returned value from "getInitialProps"', async () => { |
| 205 | + const DummyComponent = () => React.createElement("div"); |
| 206 | + const dummyInitialProps = { dummy: "dummy" }; |
| 207 | + DummyComponent.getInitialProps = async () => dummyInitialProps; |
| 208 | + const ComponentWithSecureHeaders = withSecureHeaders()(DummyComponent); |
| 209 | + |
| 210 | + const dummyContext: any = { ctx: { res: { setHeader: jest.fn(), headersSent: true } } }; |
| 211 | + await expect(ComponentWithSecureHeaders.getInitialProps!(dummyContext)).resolves.toEqual(dummyInitialProps); |
| 212 | + }); |
| 213 | + }); |
191 | 214 | }); |
192 | 215 | }); |
0 commit comments