Skip to content

Commit 78418e4

Browse files
adding more tests
1 parent dd9c413 commit 78418e4

File tree

3 files changed

+134
-11
lines changed

3 files changed

+134
-11
lines changed

Diff for: src/plugin/composables/__tests__/bindings.spec.cy.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {
2+
useBindingSettings
3+
} from '../bindings';
4+
5+
6+
describe('Bindings Composable', () => {
7+
8+
describe('useBindingSettings', () => {
9+
const settingsThatShouldBeExcluded = {
10+
columns: { sm: 6, md: 4, lg: 3, xl: 2 },
11+
options: [],
12+
required: true,
13+
rules: ['required'],
14+
when: () => { },
15+
};
16+
17+
const settings = {
18+
...settingsThatShouldBeExcluded,
19+
class: 'foo',
20+
color: 'primary',
21+
};
22+
23+
function checkExcludedSettings(result: any) {
24+
expect(result).to.not.have.property('columns');
25+
expect(result).to.not.have.property('options');
26+
expect(result).to.not.have.property('required');
27+
expect(result).to.not.have.property('rules');
28+
expect(result).to.not.have.property('when');
29+
}
30+
31+
it('should exclude settings in excludedSettings array by default', () => {
32+
const result = useBindingSettings(settings);
33+
34+
// Ensure the excluded settings are not present //
35+
checkExcludedSettings(result);
36+
37+
// Ensure the allowed settings are present //
38+
expect(result).to.have.property('class', 'foo');
39+
expect(result).to.have.property('color', 'primary');
40+
});
41+
42+
it('should exclude settings from the exclude parameter', () => {
43+
const exclude = ['color'];
44+
45+
const result = useBindingSettings(settings, exclude);
46+
47+
// Ensure the excluded settings are not present //
48+
checkExcludedSettings(result);
49+
50+
// Ensure the allowed settings are present //
51+
expect(result).to.have.property('class', 'foo');
52+
53+
// Ensure the additional excluded settings are not present //
54+
expect(result).to.not.have.property('color');
55+
});
56+
57+
it('should not exclude any settings when exclude parameter is empty', () => {
58+
const exclude: string[] = ['class']; // No settings to exclude
59+
60+
const result = useBindingSettings(settings, exclude);
61+
62+
// Ensure the excluded settings are not present //
63+
checkExcludedSettings(result);
64+
65+
// Ensure the allowed settings are present //
66+
expect(result).to.have.property('color', 'primary');
67+
68+
// Ensure the additional excluded settings are not present //
69+
expect(result).to.not.have.property('class');
70+
});
71+
});
72+
});
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {
2+
useOnActions
3+
} from '../validation';
4+
import type {
5+
Field,
6+
} from '../../types';
7+
import type { SinonStub } from 'cypress/types/sinon';
8+
9+
10+
describe('Validation Composable', () => {
11+
12+
describe('useOnActions', () => {
13+
const actions = ['blur', 'change', 'input', 'click'] as const;
14+
let emitSpy: SinonStub<any[], any>;
15+
let validateStub: SinonStub<any[], any>;
16+
let field: Field;
17+
let options: {
18+
action: ValidateAction;
19+
emit: any;
20+
field: Field;
21+
settingsValidateOn: Settings['validateOn'];
22+
validate: any;
23+
};
24+
25+
beforeEach(() => {
26+
emitSpy = cy.stub();
27+
validateStub = cy.stub().resolves();
28+
field = { name: 'foo', validateOn: undefined };
29+
options = {
30+
action: 'blur',
31+
emit: emitSpy,
32+
field,
33+
settingsValidateOn: undefined,
34+
validate: validateStub,
35+
};
36+
});
37+
38+
const runValidationTest = () => {
39+
cy.wrap(null).then(() => useOnActions(options)).then(() => {
40+
expect(validateStub).to.have.been.calledOnce;
41+
expect(emitSpy).to.have.been.calledWith('validate', field);
42+
});
43+
};
44+
45+
actions.forEach((action) => {
46+
it(`should call validate and emit for "${action}" action action when validateOn is "${action}"`, () => {
47+
options.action = action;
48+
options.settingsValidateOn = action;
49+
field.validateOn = action;
50+
runValidationTest();
51+
});
52+
});
53+
54+
it('should not call validate or emit for unrelated actions', () => {
55+
options.action = 'keydown' as any;
56+
cy.wrap(null).then(() => useOnActions(options)).then(() => {
57+
expect(validateStub).not.to.have.been.called;
58+
expect(emitSpy).not.to.have.been.called;
59+
});
60+
});
61+
});
62+
});

Diff for: src/plugin/composables/bindings.ts

-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ const excludedSettings: string[] = [
77
];
88

99

10-
// export const useBindingSettings = (settings: object, exclude?: string[]): object => {
11-
// let bindingSettings = settings;
12-
13-
// bindingSettings = Object.entries(bindingSettings).filter(([key]) => {
14-
// return !excludedSettings.includes(key) && !exclude?.includes(key);
15-
// });
16-
17-
// return Object.fromEntries(bindingSettings);
18-
// };
19-
20-
2110
export const useBindingSettings = (
2211
settings: Partial<Settings>,
2312
exclude: string[] = [],

0 commit comments

Comments
 (0)