Skip to content

Commit

Permalink
[test] Add tests for actions.Transformation
Browse files Browse the repository at this point in the history
This adds the unit tests for openhab#180.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 committed Dec 5, 2022
1 parent fc13611 commit 6b4f213
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
52 changes: 50 additions & 2 deletions test/actions.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-undef */

const { ScriptExecution } = require('../actions');
const { JavaScriptExecution } = require('./openhab.mock');
const { ScriptExecution, Transformation } = require('../actions');
const { JavaScriptExecution, JavaTransformation } = require('./openhab.mock');

jest.mock('../osgi');
jest.mock('@runtime/osgi', () => ({}), { virtual: true });
Expand Down Expand Up @@ -64,4 +64,52 @@ describe('actions.js', () => {
expect(JavaScriptExecution.createTimer).toHaveBeenCalledWith(identifier, instant);
});
});

describe('Transformation', () => {
describe('transform', () => {
const type = 'MAP';
const fn = 'en.map';
const value = 'ON';

it('throws TypeError when a wrong argument is passed.', () => {
expect(() => Transformation.transform({}, fn, value)).toThrow(TypeError);
expect(() => Transformation.transform(type, {}, value)).toThrow(TypeError);
expect(() => Transformation.transform(type, fn, { value })).toThrow(TypeError);
expect(JavaTransformation.transform).not.toHaveBeenCalled();
});

it('delegates to Java Transformation.', () => {
const type = 'MAP';
const fn = 'en.map';
const value = 'ON';

Transformation.transform(type, fn, value);

expect(JavaTransformation.transform).toHaveBeenCalledWith(type, fn, value);
});
});

describe('transformRaw', () => {
const type = 'MAP';
const fn = 'en.map';
const value = 'ON';

it('throws TypeError when a wrong argument is passed.', () => {
expect(() => Transformation.transformRaw({}, fn, value)).toThrow(TypeError);
expect(() => Transformation.transformRaw(type, {}, value)).toThrow(TypeError);
expect(() => Transformation.transformRaw(type, fn, { value })).toThrow(TypeError);
expect(JavaTransformation.transformRaw).not.toHaveBeenCalled();
});

it('delegates to Java Transformation.', () => {
const type = 'MAP';
const fn = 'en.map';
const value = 'ON';

Transformation.transformRaw(type, fn, value);

expect(JavaTransformation.transformRaw).toHaveBeenCalledWith(type, fn, value);
});
});
});
});
3 changes: 2 additions & 1 deletion test/java.mock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ModuleBuilder, Configuration, JavaScriptExecution } = require('./openhab.mock');
const { ModuleBuilder, Configuration, JavaScriptExecution, JavaTransformation } = require('./openhab.mock');

class HashSet {
add () {}
Expand Down Expand Up @@ -39,6 +39,7 @@ const TYPES = {
'org.openhab.core.automation.util.ModuleBuilder': ModuleBuilder,
'org.openhab.core.config.core.Configuration': Configuration,
'org.openhab.core.model.script.actions.ScriptExecution': JavaScriptExecution,
'org.openhab.core.transform.actions.Transformation': JavaTransformation,
'org.osgi.framework.FrameworkUtil': FrameworkUtil,
'org.slf4j.LoggerFactory': LoggerFactory
};
Expand Down
6 changes: 5 additions & 1 deletion test/openhab.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ class JavaScriptExecution {
static createTimer () {}
}

module.exports = { Configuration, ModuleBuilder, JavaScriptExecution };
class JavaTransformation {}
JavaTransformation.transform = jest.fn(() => 'on');
JavaTransformation.transformRaw = jest.fn(() => 'on');

module.exports = { Configuration, ModuleBuilder, JavaScriptExecution, JavaTransformation };

0 comments on commit 6b4f213

Please sign in to comment.