Skip to content

Commit 2f06e6d

Browse files
authored
Test for js/js-export/interface.js (#4452)
Signed-off-by: Justin Charles <[email protected]>
1 parent 0903cef commit 2f06e6d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const JSInterface = require('../interface');
2+
JSInterface._methodArgConstraints = {};
3+
4+
describe('JSInterface', () => {
5+
describe('isClampBlock', () => {
6+
it('should return true for a known clamp block', () => {
7+
expect(JSInterface.isClampBlock('newnote')).toBe(true);
8+
});
9+
10+
it('should return false for an unknown block', () => {
11+
expect(JSInterface.isClampBlock('nonexistent')).toBe(false);
12+
});
13+
});
14+
15+
describe('isSetter', () => {
16+
it('should return true for a valid setter block', () => {
17+
expect(JSInterface.isSetter('pickup')).toBe(true);
18+
});
19+
20+
it('should return false for a block that is not a setter', () => {
21+
expect(JSInterface.isSetter('newnote')).toBe(false);
22+
});
23+
});
24+
25+
describe('isGetter', () => {
26+
it('should return true for a valid getter block', () => {
27+
expect(JSInterface.isGetter('mynotevalue')).toBe(true);
28+
});
29+
30+
it('should return false for a block that is not a getter', () => {
31+
expect(JSInterface.isGetter('pickup')).toBe(false);
32+
});
33+
});
34+
35+
describe('isMethod', () => {
36+
it('should return true for a valid method block', () => {
37+
expect(JSInterface.isMethod('newnote')).toBe(true);
38+
});
39+
40+
it('should return false for a block that is not a method', () => {
41+
expect(JSInterface.isMethod('pickup')).toBe(false);
42+
});
43+
});
44+
45+
describe('methodReturns', () => {
46+
it('should return true for a method that has a return value', () => {
47+
expect(JSInterface.methodReturns('getDict')).toBe(true);
48+
});
49+
50+
it('should return false for a method that does not have a return value', () => {
51+
expect(JSInterface.methodReturns('newnote')).toBe(false);
52+
});
53+
});
54+
55+
describe('getSetterName', () => {
56+
it('should return the correct setter name when available', () => {
57+
expect(JSInterface.getSetterName('pickup')).toBe('PICKUP');
58+
});
59+
60+
it('should return null when no setter exists for the given block', () => {
61+
expect(JSInterface.getSetterName('newnote')).toBeNull();
62+
});
63+
});
64+
65+
describe('getGetterName', () => {
66+
it('should return the correct getter name when available', () => {
67+
expect(JSInterface.getGetterName('mynotevalue')).toBe('NOTEVALUE');
68+
});
69+
70+
it('should return null when no getter exists for the given block', () => {
71+
expect(JSInterface.getGetterName('pickup')).toBeNull();
72+
});
73+
});
74+
75+
describe('getMethodName', () => {
76+
it('should return the correct method name when available', () => {
77+
expect(JSInterface.getMethodName('newnote')).toBe('playNote');
78+
});
79+
80+
it('should return null when no method exists for the given block', () => {
81+
expect(JSInterface.getMethodName('pickup')).toBeNull();
82+
});
83+
});
84+
85+
describe('rearrangeMethodArgs', () => {
86+
it('should rearrange the arguments for methods present in the lookup', () => {
87+
const args = [1, 2, 3];
88+
expect(JSInterface.rearrangeMethodArgs('setDict', args)).toEqual([2, 3, 1]);
89+
});
90+
91+
it('should return the original args if no rearrangement is required', () => {
92+
const args = [1, 2, 3];
93+
expect(JSInterface.rearrangeMethodArgs('nonExistingMethod', args)).toEqual(args);
94+
});
95+
});
96+
97+
describe('validateArgs', () => {
98+
it('should return the original args when no constraints are defined for the method', () => {
99+
const args = [1, 2, 3];
100+
expect(JSInterface.validateArgs('nonExistingMethod', args)).toEqual(args);
101+
});
102+
});
103+
});

js/js-export/interface.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,7 @@ class JSInterface {
634634
return finalArgs;
635635
}
636636
}
637+
if (typeof module !== 'undefined' && module.exports) {
638+
module.exports = JSInterface;
639+
}
640+

0 commit comments

Comments
 (0)