Skip to content

Commit

Permalink
Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Apr 8, 2022
1 parent d1360a8 commit d57b528
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 9 deletions.
4 changes: 0 additions & 4 deletions src/schema/data/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
*/

export const SET_SCHEMA_CURRENT_DEFINITION_NAME = 'SET_SCHEMA_CURRENT_DEFINITION_NAME';
export const UPDATE_SCHEMA_STATE = 'UPDATE_SCHEMA_STATE';

export function setSchemaCurrentDefinitionName( currentSchemaDefinitionName ) {
return { type: SET_SCHEMA_CURRENT_DEFINITION_NAME, currentSchemaDefinitionName };
}

export function updateSchemaState() {
return { type: UPDATE_SCHEMA_STATE };
}
4 changes: 1 addition & 3 deletions src/schema/data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
*/

import {
SET_SCHEMA_CURRENT_DEFINITION_NAME,
UPDATE_SCHEMA_STATE
SET_SCHEMA_CURRENT_DEFINITION_NAME
} from './actions';

import {
Expand Down Expand Up @@ -42,7 +41,6 @@ export default function schemaReducer( globalState, schemaState, action ) {
// if we're back in the commands tab.
// * UPDATE_MODEL_STATE – An action called by the editorEventObserver for the model document change.
case SET_ACTIVE_INSPECTOR_TAB:
case UPDATE_SCHEMA_STATE:
return {
...schemaState,

Expand Down
3 changes: 1 addition & 2 deletions src/schema/pane.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { updateSchemaState } from './data/actions';

import Pane from '../components/pane';
import Tabs from '../components/tabs';
Expand Down Expand Up @@ -37,4 +36,4 @@ const mapStateToProps = ( { currentEditorName } ) => {
return { currentEditorName };
};

export default connect( mapStateToProps, { updateSchemaState } )( SchemaPane );
export default connect( mapStateToProps )( SchemaPane );
18 changes: 18 additions & 0 deletions tests/inspector/schema/data/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

import {
setSchemaCurrentDefinitionName,
SET_SCHEMA_CURRENT_DEFINITION_NAME
} from '../../../../src/schema/data/actions';

describe( 'schema data store actions', () => {
it( 'should export setSchemaCurrentDefinitionName()', () => {
expect( setSchemaCurrentDefinitionName( 'foo' ) ).to.deep.equal( {
type: SET_SCHEMA_CURRENT_DEFINITION_NAME,
currentSchemaDefinitionName: 'foo'
} );
} );
} );
180 changes: 180 additions & 0 deletions tests/inspector/schema/data/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* global document, window */

import TestEditor from '../../../utils/testeditor';
import schemaReducer from '../../../../src/schema/data/reducer';

import {
setSchemaCurrentDefinitionName
} from '../../../../src/schema/data/actions';

import {
setActiveTab,
setEditors,
setCurrentEditorName
} from '../../../../src/data/actions';

import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';

describe( 'schema data store reducer', () => {
let editorA, editorB;
let elementA, elementB;
let globalState, schemaState;

beforeEach( async () => {
window.localStorage.clear();

elementA = document.createElement( 'div' );
elementB = document.createElement( 'div' );
document.body.appendChild( elementA );
document.body.appendChild( elementB );

editorA = await TestEditor.create( elementA, {
plugins: [ Paragraph ],
initialData: '<p>foo</p>'
} );

editorB = await TestEditor.create( elementB );

globalState = {
currentEditorName: 'a',
editors: new Map( [
[ 'a', editorA ],
[ 'b', editorB ]
] ),
ui: {
activeTab: 'Schema'
}
};

schemaState = schemaReducer( globalState, null, {} );
} );

afterEach( async () => {
await editorA.destroy();
await editorB.destroy();
} );

it( 'should not create a state if ui#activeTab is different than "Schema"', () => {
globalState.ui.activeTab = 'Model';

schemaState = schemaReducer( globalState, null, {} );

expect( schemaState ).to.be.null;
} );

it( 'should create a default state if no schema state was passed to the reducer', () => {
schemaState = schemaReducer( globalState, null, {} );

expect( schemaState ).to.have.property( 'treeDefinition' );
expect( schemaState ).to.have.property( 'currentSchemaDefinitionName' );
expect( schemaState ).to.have.property( 'currentSchemaDefinition' );
} );

it( 'should pass through when no action was passed to the reducer', () => {
schemaState = schemaReducer( globalState, {
treeDefinition: [ 'foo' ],
currentSchemaDefinitionName: 'bar',
currentSchemaDefinition: 'baz'
}, {} );

expect( schemaState.treeDefinition ).to.deep.equal( [ 'foo' ] );
expect( schemaState.currentSchemaDefinitionName ).to.equal( 'bar' );
expect( schemaState.currentSchemaDefinition ).to.equal( 'baz' );
} );

describe( 'application state', () => {
describe( '#currentSchemaDefinitionName', () => {
it( 'should be reset on setEditors() action', () => {
schemaState.currentSchemaDefinitionName = 'paragraph';
schemaState = schemaReducer( globalState, schemaState, setEditors( new Map( [ [ 'b', editorB ] ] ) ) );

expect( schemaState.currentSchemaDefinitionName ).to.be.null;
} );

it( 'should be reset on setCurrentEditorName() action', () => {
schemaState.currentSchemaDefinitionName = null;
schemaState = schemaReducer( globalState, schemaState, setCurrentEditorName( 'b' ) );

expect( schemaState.currentSchemaDefinitionName ).to.be.null;
} );

it( 'should be set on setSchemaCurrentDefinitionName() action', () => {
schemaState.currentSchemaDefinitionName = null;
schemaState = schemaReducer( globalState, schemaState, setSchemaCurrentDefinitionName( 'paragraph' ) );

expect( schemaState.currentSchemaDefinitionName ).to.equal( 'paragraph' );
} );
} );

describe( '#currentSchemaDefinition', () => {
it( 'should be reset on setEditors() action', () => {
schemaState.currentSchemaDefinition = null;
schemaState = schemaReducer( globalState, schemaState, setEditors( new Map( [ [ 'b', editorB ] ] ) ) );

expect( schemaState.currentSchemaDefinition ).to.be.null;
} );

it( 'should be reset on setCurrentEditorName() action', () => {
schemaState.currentSchemaDefinition = null;
schemaState = schemaReducer( globalState, schemaState, setCurrentEditorName( 'b' ) );

expect( schemaState.currentSchemaDefinition ).to.be.null;
} );

it( 'should be set on setSchemaCurrentDefinitionName() action', () => {
schemaState.currentSchemaDefinition = null;
schemaState = schemaReducer( globalState, schemaState, setSchemaCurrentDefinitionName( 'paragraph' ) );

expect( schemaState.currentSchemaDefinition ).to.be.an( 'object' );
} );

it( 'should be set on setActiveTab() action', () => {
schemaState.currentSchemaDefinitionName = 'paragraph';
schemaState.currentSchemaDefinition = null;
schemaState = schemaReducer( globalState, schemaState, setActiveTab( 'Schema' ) );

expect( schemaState.currentSchemaDefinition ).to.be.an( 'object' );
} );
} );

describe( '#treeDefinition', () => {
it( 'should be empty if there are no editors', () => {
schemaState.treeDefinition = null;

globalState.editors = new Map();
globalState.currentEditorName = null;

schemaState = schemaReducer( globalState, schemaState, setEditors( new Map() ) );

expect( schemaState.treeDefinition ).to.be.an( 'array' );
expect( schemaState.treeDefinition ).to.have.length( 0 );
} );

it( 'should be set on setEditors() action', () => {
schemaState.treeDefinition = null;
schemaState = schemaReducer( globalState, schemaState, setEditors( new Map( [ [ 'b', editorB ] ] ) ) );

expect( schemaState.treeDefinition ).to.be.an( 'array' );
} );

it( 'should be set on setCurrentEditorName() action', () => {
schemaState.treeDefinition = null;
schemaState = schemaReducer( globalState, schemaState, setCurrentEditorName( 'b' ) );

expect( schemaState.treeDefinition ).to.be.an( 'array' );
} );

it( 'should be set on setActiveTab() action', () => {
schemaState.treeDefinition = null;
schemaState = schemaReducer( globalState, schemaState, setActiveTab( 'Commands' ) );

expect( schemaState.treeDefinition ).to.be.an( 'array' );
} );
} );
} );
} );
81 changes: 81 additions & 0 deletions tests/inspector/schema/pane.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* global document, window */

import React from 'react';
import TestEditor from '../../utils/testeditor';
import { createStore } from 'redux';
import { Provider } from 'react-redux';

import SchemaPane from '../../../src/schema/pane';
import SchemaTree from '../../../src/schema/tree';
import SchemaDefinitionInspector from '../../../src/schema/schemadefinitioninspector';

describe( '<SchemaPane />', () => {
let editor, wrapper, element, store;

beforeEach( () => {
window.localStorage.clear();

element = document.createElement( 'div' );
document.body.appendChild( element );

return TestEditor.create( element ).then( newEditor => {
editor = newEditor;

store = createStore( state => state, {
editors: new Map( [ [ 'test-editor', editor ] ] ),
currentEditorName: 'test-editor',
ui: {
activeTab: 'Schema'
},
schema: {
}
} );

wrapper = mount( <Provider store={store}><SchemaPane /></Provider> );
} );
} );

afterEach( () => {
wrapper.unmount();
element.remove();

return editor.destroy();
} );

describe( 'render()', () => {
it( 'renders a placeholder when no props#currentEditorName', () => {
store = createStore( state => state, {
currentEditorName: null,
model: {
ui: {}
}
} );

const wrapper = mount( <Provider store={store}><SchemaPane /></Provider> );

expect( wrapper.text() ).to.match( /^Nothing to show/ );

wrapper.unmount();
} );

it( 'should render <Tabs>', () => {
const tabs = wrapper.find( 'Tabs' );

expect( tabs ).to.have.length( 1 );
expect( tabs.props().activeTab ).to.equal( 'Inspect' );
} );

it( 'should render a <SchemaTree/>', () => {
expect( wrapper.find( SchemaTree ) ).to.have.length( 1 );
} );

it( 'should render a <SchemaDefinitionInspector/>', () => {
expect( wrapper.find( SchemaDefinitionInspector ) ).to.have.length( 1 );
} );
} );
} );
Loading

0 comments on commit d57b528

Please sign in to comment.