Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions resources/js/components/field-validation/Builder.vue
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@
v-model="customRule"
ref="customRuleInput"
@keydown.enter.prevent="add(customRule)"
@blur="add(customRule)"
@focusout="add(customRule)"
/>

<sortable-list
Expand DownExpand Up@@ -238,7 +238,7 @@ export default {
this.rules.push(rule);
}

this.$nextTick(() => this.$refs.rulesSelect.focus());
this.$nextTick(() => this.$refs.rulesSelect?.focus());
},

add(rule) {
Expand All@@ -248,7 +248,7 @@ export default {
this.resetState();
this.selectedLaravelRule = rule;
this.customRule = rule;
this.$nextTick(() => this.$refs.customRuleInput.focus());
this.$nextTick(() => this.$refs.customRuleInput?.focus());
} else {
this.ensure(rule);
}
Expand Down
50 changes: 50 additions & 0 deletions resources/js/tests/components/field-validation/Builder.test.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
import { mount } from '@vue/test-utils';
import { expect, test, vi } from 'vitest';
import Builder from '@/components/field-validation/Builder.vue';

vi.mock('@inertiajs/vue3', () => ({
usePage: () => ({ props: { extensionRules: [] } }),
}));

globalThis.__ = (key) => key;
globalThis.__n = (key) => key;
globalThis.clone = (value) => structuredClone(value);

const mountBuilder = () =>
mount(Builder, {
props: { config: {} },
global: {
mocks: {
$config: { get: () => '12.0.0' },
},
stubs: {
SortableList: true,
},
},
});

test('a typed rule parameter is committed when the input loses focus', async () => {
const wrapper = mountBuilder();

// Picking a parameterized rule swaps the combobox for a plain input holding "after:"
wrapper.vm.add('after:');
await wrapper.vm.$nextTick();

const input = wrapper.find('input');
await input.setValue('after:{this}.start_time');
await input.trigger('focusout');

expect(wrapper.emitted('updated').at(-1)[0]).toEqual(['after:{this}.start_time']);
});

test('an unfinished rule is not committed when the input loses focus', async () => {
const wrapper = mountBuilder();

wrapper.vm.add('after:');
await wrapper.vm.$nextTick();

const emitsBefore = wrapper.emitted('updated')?.length ?? 0;
await wrapper.find('input').trigger('focusout');

expect(wrapper.emitted('updated')?.length ?? 0).toBe(emitsBefore);
});
Loading