From e15f1aab239802f5e3409ece2c44fd853f8d5912 Mon Sep 17 00:00:00 2001 From: Gervwyk Date: Mon, 26 Oct 2020 21:38:29 +0200 Subject: [PATCH] feat(blockTools): tests for LogoSpinner and mockBlockProps, and extra lines --- packages/blockTools/jest.config.js | 4 +- .../blockTools/src/Skeleton/SkeletonInput.js | 2 +- .../src/Spinner/LogoSpinner.test.js | 119 ++++++ .../blockTools/src/mockBlockProps.test.js | 365 +++++++++++++++++- .../blockTools/src/useRunAfterUpdate.test.js | 18 +- 5 files changed, 499 insertions(+), 9 deletions(-) create mode 100644 packages/blockTools/src/Spinner/LogoSpinner.test.js diff --git a/packages/blockTools/jest.config.js b/packages/blockTools/jest.config.js index 547a23c89..a4608ded3 100644 --- a/packages/blockTools/jest.config.js +++ b/packages/blockTools/jest.config.js @@ -6,8 +6,8 @@ module.exports = { coveragePathIgnorePatterns: [ '/dist/', '/tests/', - 'runRenderTests', - 'mockBlock', + 'runRenderTests.js', + 'mockBlock.js', ], coverageReporters: ['lcov', 'text', 'clover'], errorOnDeprecated: true, diff --git a/packages/blockTools/src/Skeleton/SkeletonInput.js b/packages/blockTools/src/Skeleton/SkeletonInput.js index 7e797e6ee..328f81529 100644 --- a/packages/blockTools/src/Skeleton/SkeletonInput.js +++ b/packages/blockTools/src/Skeleton/SkeletonInput.js @@ -46,7 +46,7 @@ const SkeletonInput = ({ properties, methods }) => { methods={methods} properties={{ width: properties.width || '100%', - height: properties.inputHeight || inputHeight || 32, + height: properties.inputHeight || inputHeight, }} /> diff --git a/packages/blockTools/src/Spinner/LogoSpinner.test.js b/packages/blockTools/src/Spinner/LogoSpinner.test.js new file mode 100644 index 000000000..02e397039 --- /dev/null +++ b/packages/blockTools/src/Spinner/LogoSpinner.test.js @@ -0,0 +1,119 @@ +/* + Copyright 2020 Lowdefy, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import React from 'react'; +import LogoSpinner from './LogoSpinner'; +import { create, act } from 'react-test-renderer'; + +test('default', () => { + let comp; + act(() => { + comp = create(); + }); + expect(comp.toJSON()).toMatchInlineSnapshot(` + + + + + + + + `); +}); + +test('color props', () => { + let comp; + act(() => { + comp = create(); + }); + expect(comp.toJSON()).toMatchInlineSnapshot(` + + + + + + + + `); +}); diff --git a/packages/blockTools/src/mockBlockProps.test.js b/packages/blockTools/src/mockBlockProps.test.js index 4f5044246..763573d00 100644 --- a/packages/blockTools/src/mockBlockProps.test.js +++ b/packages/blockTools/src/mockBlockProps.test.js @@ -14,6 +14,367 @@ limitations under the License. */ -test('a', () => { - expect(1).toBe(1); +import mockBlockProps from './mockBlockProps'; + +test('basic display', () => { + const config = { + id: 'a', + type: 'Display', + }; + const meta = { + category: 'display', + }; + expect(mockBlockProps(config, meta)).toEqual({ blockId: 'a', id: 'a', type: 'Display' }); +}); + +test('basic input', () => { + const config = { + id: 'a', + type: 'Input', + }; + const meta = { + category: 'input', + }; + expect(mockBlockProps(config, meta)).toEqual({ blockId: 'a', id: 'a', type: 'Input' }); +}); + +test('basic container', () => { + const config = { + id: 'a', + type: 'Container', + }; + const meta = { + category: 'container', + }; + expect(mockBlockProps(config, meta)).toEqual({ blockId: 'a', id: 'a', type: 'Container' }); +}); + +test('basic context', () => { + const config = { + id: 'a', + type: 'Context', + }; + const meta = { + category: 'context', + }; + expect(mockBlockProps(config, meta)).toEqual({ blockId: 'a', id: 'a', type: 'Context' }); +}); + +test('basic list', () => { + const config = { + id: 'a', + type: 'List', + }; + const meta = { + category: 'list', + }; + expect(mockBlockProps(config, meta)).toEqual({ blockId: 'a', id: 'a', type: 'List' }); +}); + +test('blocks container', () => { + const config = { + id: 'a', + type: 'Container', + blocks: [ + { + id: 'b', + type: 'Test', + }, + ], + }; + const meta = { + category: 'container', + }; + const res = mockBlockProps(config, meta); + expect(res).toMatchInlineSnapshot(` + Object { + "areas": Object { + "content": Array [ + Object { + "id": "b", + "type": "Test", + }, + ], + }, + "blockId": "a", + "blocks": Array [ + Object { + "id": "b", + "type": "Test", + }, + ], + "content": Object { + "content": [Function], + }, + "id": "a", + "type": "Container", + } + `); + expect(res.content.content()).toMatchInlineSnapshot(` +
+ content +
+ `); +}); +test('blocks areas container', () => { + const config = { + id: 'a', + type: 'Container', + blocks: [ + { + id: 'b', + type: 'Test', + }, + ], + areas: { + content: [ + { + id: 'x', + type: 'Test', + }, + ], + }, + }; + const meta = { + category: 'container', + }; + + const res = mockBlockProps(config, meta); + expect(res).toMatchInlineSnapshot(` + Object { + "areas": Object { + "content": Array [ + Object { + "id": "b", + "type": "Test", + }, + ], + }, + "blockId": "a", + "blocks": Array [ + Object { + "id": "b", + "type": "Test", + }, + ], + "content": Object { + "content": [Function], + }, + "id": "a", + "type": "Container", + } + `); + expect(res.content.content()).toMatchInlineSnapshot(` +
+ content +
+ `); +}); + +test('areas container', () => { + const config = { + id: 'a', + type: 'Container', + areas: { + content: [ + { + id: 'b', + type: 'Test', + }, + ], + }, + }; + const meta = { + category: 'container', + }; + const res = mockBlockProps(config, meta); + expect(res).toMatchInlineSnapshot(` + Object { + "areas": Object { + "content": Array [ + Object { + "id": "b", + "type": "Test", + }, + ], + }, + "blockId": "a", + "content": Object { + "content": [Function], + }, + "id": "a", + "type": "Container", + } + `); + expect(res.content.content()).toMatchInlineSnapshot(` +
+ content +
+ `); +}); + +test('areas context', () => { + const config = { + id: 'a', + type: 'Context', + areas: { + content: [ + { + id: 'b', + type: 'Test', + }, + ], + }, + }; + const meta = { + category: 'context', + }; + const res = mockBlockProps(config, meta); + expect(res).toMatchInlineSnapshot(` + Object { + "areas": Object { + "content": Array [ + Object { + "id": "b", + "type": "Test", + }, + ], + }, + "blockId": "a", + "content": Object { + "content": [Function], + }, + "id": "a", + "type": "Context", + } + `); + expect(res.content.content()).toMatchInlineSnapshot(` +
+ content +
+ `); +}); + +test('areas list', () => { + const config = { + id: 'a', + type: 'List', + areas: { + content: [ + { + id: 'b', + type: 'Test', + }, + ], + }, + }; + const meta = { + category: 'list', + }; + const res = mockBlockProps(config, meta); + expect(res).toMatchInlineSnapshot(` + Object { + "areas": Object { + "content": Array [ + Object { + "id": "b", + "type": "Test", + }, + ], + }, + "blockId": "a", + "id": "a", + "list": Object { + "content": [Function], + }, + "type": "List", + } + `); + expect(res.list.content()).toMatchInlineSnapshot(` + Array [ +
+ b +
, + ] + `); +}); + +test('actions display', () => { + global.alert = jest.fn(); + const config = { + id: 'a', + type: 'Display', + actions: { + onClick: [ + { + id: 'c', + type: 'Test', + }, + ], + }, + }; + const meta = { + category: 'display', + }; + const res = mockBlockProps(config, meta); + expect(res).toMatchInlineSnapshot(` + Object { + "actions": Object { + "onClick": Array [ + Object { + "id": "c", + "type": "Test", + }, + ], + }, + "blockId": "a", + "id": "a", + "methods": Object { + "callAction": [Function], + }, + "type": "Display", + } + `); + res.methods.callAction({ action: 'click' }); + expect(global.alert).toBeCalledWith(JSON.stringify({ action: 'click' }, null, 2)); }); diff --git a/packages/blockTools/src/useRunAfterUpdate.test.js b/packages/blockTools/src/useRunAfterUpdate.test.js index 7dfbabb5a..466fd62a7 100644 --- a/packages/blockTools/src/useRunAfterUpdate.test.js +++ b/packages/blockTools/src/useRunAfterUpdate.test.js @@ -15,10 +15,10 @@ */ import useRunAfterUpdate from './useRunAfterUpdate'; -import { useRef } from 'react'; +import { useRef, useLayoutEffect } from 'react'; jest.mock('react', () => { - const useLayoutEffect = (fn) => fn(); + const useLayoutEffect = jest.fn(); const ref = { current: jest.fn() }; const useRef = () => ref; return { useLayoutEffect, useRef }; @@ -29,11 +29,21 @@ const { current } = ref; beforeEach(() => { ref.current.mockReset(); + useLayoutEffect.mockReset(); + useLayoutEffect.mockImplementation((fn) => fn()); }); test('default call', () => { const res = useRunAfterUpdate(); + expect(useLayoutEffect).toHaveBeenCalledTimes(1); + expect(current).toBeCalledTimes(1); + res(() => 'one'); + expect(ref.current()).toEqual('one'); + useRunAfterUpdate(); + expect(useLayoutEffect).toHaveBeenCalledTimes(2); + expect(current).toBeCalledTimes(1); + useRunAfterUpdate(); + expect(useLayoutEffect).toHaveBeenCalledTimes(3); + expect(ref.current).toEqual(null); expect(current).toBeCalledTimes(1); - res('one'); - expect(ref.current).toEqual('one'); });