From 7a0b3d7fa271d5efe1b07dfe66303e83c0ac2dc9 Mon Sep 17 00:00:00 2001 From: Henrik Wenz Date: Thu, 9 Aug 2018 10:10:50 +0200 Subject: [PATCH] Add unit tests for loadGetInitialProps --- test/unit/loadGetInitialProps.test.js | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/unit/loadGetInitialProps.test.js diff --git a/test/unit/loadGetInitialProps.test.js b/test/unit/loadGetInitialProps.test.js new file mode 100644 index 0000000000000..860e90406b64c --- /dev/null +++ b/test/unit/loadGetInitialProps.test.js @@ -0,0 +1,52 @@ +/* global describe, it, expect */ +import { loadGetInitialProps } from '../../dist/lib/utils' + +describe('loadGetInitialProps', () => { + it('should throw if getInitialProps is defined as an instance method', () => { + class TestComponent { + getInitialProps () {} + } + const rejectPromise = loadGetInitialProps(TestComponent, {}) + const error = new Error('"TestComponent.getInitialProps()" is defined as an instance method - visit https://err.sh/next.js/get-inital-props-as-an-instance-method for more information.') + return expect(rejectPromise).rejects.toEqual(error) + }) + + it('should resolve to an object if getInitialProps is missing', async () => { + const result = await loadGetInitialProps(() => {}, {}) + expect(result).toEqual({}) + }) + + it('should resolve getInitialProps', async () => { + class TestComponent { + static async getInitialProps () { + return { foo: 1 } + } + } + const result = await loadGetInitialProps(TestComponent, {}) + expect(result).toEqual({ foo: 1 }) + }) + + it('should be able to return an invalid value if the request was already sent', async () => { + class TestComponent { + static async getInitialProps () { + return 'invalidValue' + } + } + const ctx = { + res: { + finished: true + } + } + const result = await loadGetInitialProps(TestComponent, ctx) + expect(result).toBe('invalidValue') + }) + + it('should throw if getInitialProps won\'t return an object ', () => { + class TestComponent { + static async getInitialProps () {} + } + const rejectPromise = loadGetInitialProps(TestComponent, {}) + const error = new Error('"TestComponent.getInitialProps()" should resolve to an object. But found "undefined" instead.') + return expect(rejectPromise).rejects.toEqual(error) + }) +})