Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dev] Tools utils tests #1602

Merged
merged 7 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add tests for tools utils and coverage report
  • Loading branch information
gohabereg committed Mar 17, 2021
commit 9bacf38bc6534ee86986bd22af5f6bd58bc22512
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
"babel-plugin-add-module-exports",
"babel-plugin-class-display-name",
"@babel/plugin-transform-runtime"
]
],
"env": {
"test": {
"plugins": [ "istanbul" ]
}
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ test/cypress/screenshots
test/cypress/videos

dist/

coverage/
.nyc_output/
4 changes: 3 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"env": {
"NODE_ENV": "test"
},
"fixturesFolder": "test/cypress/fixtures",
"integrationFolder": "test/cypress/tests",
"screenshotsFolder": "test/cypress/screenshots",
"videosFolder": "test/cypress/videos",
"supportFile": "test/cypress/support/index.ts"
"supportFile": "test/cypress/support/index.ts",
"pluginsFile": "test/cypress/plugins/index.ts"
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@
"@babel/plugin-transform-runtime": "^7.9.0",
"@babel/polyfill": "^7.8.7",
"@babel/preset-env": "^7.9.5",
"@babel/preset-typescript": "^7.13.0",
"@babel/register": "^7.9.0",
"@babel/runtime": "^7.9.2",
"@codexteam/shortcuts": "^1.1.1",
"@cypress/code-coverage": "^3.9.2",
"@cypress/webpack-preprocessor": "^5.6.0",
"@types/node": "^14.14.35",
"@types/webpack": "^4.41.12",
"@types/webpack-env": "^1.15.2",
"babel-loader": "^8.1.0",
"babel-plugin-add-module-exports": "^1.0.0",
"babel-plugin-class-display-name": "^2.1.0",
"babel-plugin-istanbul": "^6.0.0",
"core-js": "3.6.5",
"css-loader": "^3.5.3",
"cssnano": "^4.1.10",
Expand Down
16 changes: 16 additions & 0 deletions test/cypress/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
// export default function(on, config): void {}
module.exports = (on, config) => {
require('@cypress/code-coverage/task')(on, config);

const preprocessor = require('@cypress/webpack-preprocessor');

const options = preprocessor.defaultOptions;

options.typescript = require.resolve('typescript');
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
options.webpackOptions = require('../../../webpack.config.js')({}, { mode: 'test' });

on('file:preprocessor', preprocessor(options));

// It's IMPORTANT to return the config object
// with any changed environment variables
return config;
};
2 changes: 2 additions & 0 deletions test/cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* behavior that modifies Cypress.
*/

import '@cypress/code-coverage/support';

/**
* File with the helpful commands
*/
Expand Down
230 changes: 230 additions & 0 deletions test/cypress/tests/tools.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// eslint-disable-next-line spaced-comment
/// <reference path="../support/index.d.ts" />

import ToolsCollection from '../../../src/components/tools/collection'
import ToolsFactory from '../../../src/components/tools/factory';
import Paragraph from '../../../src/tools/paragraph/dist/bundle';
import LinkInlineTool from '../../../src/components/inline-tools/inline-tool-link';
import InlineTool from '../../../src/components/tools/inline';
import BlockTool from '../../../src/components/tools/block';
import MoveUpTune from '../../../src/components/block-tunes/block-tune-move-up';
import BlockTune from '../../../src/components/tools/tune';
import BaseTool from '../../../src/components/tools/base';

const FakeTool = {
isBlock() {
return false;
},
isInline() {
return false;
},
isTune() {
return false;
},
isInternal: false,
};

const FakeBlockTool = {
...FakeTool,
isBlock() {
return true;
},
};

const FakeInlineTool = {
...FakeTool,
isInline() {
return true;
}
};

const FakeBlockTune = {
...FakeTool,
isTune() {
return true;
}
};

describe('Unit test Tools utilities', () => {
context('ToolsFactory', () => {
let factory;
const config = {
paragraph: {
class: Paragraph,
},
link: {
class: LinkInlineTool,
},
moveUp: {
class: MoveUpTune,
},
};

beforeEach(() => {
factory = new ToolsFactory(
config,
{
placeholder: 'Placeholder',
defaultBlock: 'paragraph'
} as any,
{} as any
);
});

context('.get', () => {
it('should return appropriate tool object', () => {
const tool = factory.get('link');

expect(tool.name).to.be.eq('link');
});

it('should return InlineTool object for inline tool', () => {
const tool = factory.get('link');

expect(tool instanceof InlineTool).to.be.true;
});

it('should return BlockTool object for block tool', () => {
const tool = factory.get('paragraph');

expect(tool instanceof BlockTool).to.be.true;
});

it('should return BlockTune object for tune', () => {
const tool = factory.get('moveUp');

expect(tool instanceof BlockTune).to.be.true;
});
});
});

context('ToolsCollection', () => {
let collection;
const fakeTools = [
['block1', FakeBlockTool],
['inline1', FakeInlineTool],
['block2', { ...FakeBlockTool, isInternal: true }],
['tune1', FakeBlockTune],
['block3', FakeBlockTool],
['inline2', { ...FakeInlineTool, isInternal: true }],
['tune2', FakeBlockTune],
['tune3', { ...FakeBlockTune, isInternal: true }],
['block3', FakeInlineTool],
['block4', FakeBlockTool],
];

beforeEach(() => {
collection = new ToolsCollection(fakeTools as any);
})

it('should be instance of Map', () => {
expect(collection instanceof Map).to.be.true;
});

context('.block', () => {
it('should return new instance of ToolsCollection', () => {
expect(collection.block instanceof ToolsCollection).to.be.true;
});

it('result should contain only block tools', () => {
expect(
Array
.from(
collection.block.values()
)
.every((tool: BlockTool) => tool.isBlock())
).to.be.true;
});
});

context('.inline', () => {
it('should return new instance of ToolsCollection', () => {
expect(collection.inline instanceof ToolsCollection).to.be.true;
});

it('result should contain only inline tools', () => {
expect(
Array
.from(
collection.inline.values()
)
.every((tool: InlineTool) => tool.isInline())
).to.be.true;
});
});

context('.tune', () => {
it('should return new instance of ToolsCollection', () => {
expect(collection.tune instanceof ToolsCollection).to.be.true;
});

it('result should contain only block tools', () => {
expect(
Array
.from(
collection.tune.values()
)
.every((tool: BlockTune) => tool.isTune())
).to.be.true;
});
});

context('.internal', () => {
it('should return new instance of ToolsCollection', () => {
expect(collection.internal instanceof ToolsCollection).to.be.true;
});

it('result should contain only internal tools', () => {
expect(
Array
.from(
collection.internal.values()
)
.every((tool: BaseTool) => tool.isInternal)
).to.be.true;
});
});

context('.external', () => {
it('should return new instance of ToolsCollection', () => {
expect(collection.external instanceof ToolsCollection).to.be.true;
});

it('result should contain only external tools', () => {
expect(
Array
.from(
collection.external.values()
)
.every((tool: BaseTool) => !tool.isInternal)
).to.be.true;
});
});

context('mixed access', () => {
context('.tune.internal', () => {
it('should return only internal tunes', () => {
expect(
Array
.from(
collection.tune.internal.values()
)
.every((tool: BlockTune) => tool.isTune() && tool.isInternal)
).to.be.true;
});
});

context('.external.block', () => {
it('should return only external block tools', () => {
expect(
Array
.from(
collection.external.block.values()
)
.every((tool: BlockTool) => tool.isBlock() && !tool.isInternal)
).to.be.true;
});
});
});
})
});
11 changes: 7 additions & 4 deletions test/cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"compilerOptions": {
"target": "es2017",
"lib": ["es2017", "dom"],
"types": ["cypress"]
"lib": ["dom", "es2017", "es2018"],
"moduleResolution": "node",
"resolveJsonModule": true,

"allowSyntheticDefaultImports": true
},
"include": [
"**/*.ts"
"../../**/*.ts"
]
}
}
Loading