mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-16 09:43:18 +01:00
Fix timezone mismatch modal posting to the removed Jetstream route
Use useUpdateUserMutation, matching UpdateProfileInformationForm.
This commit is contained in:
committed by
Gregor Vostrak
parent
602a8daa1f
commit
7831bc697e
@@ -0,0 +1,72 @@
|
|||||||
|
import { flushPromises, shallowMount } from '@vue/test-utils';
|
||||||
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
import { createPinia } from 'pinia';
|
||||||
|
import { VueQueryPlugin } from '@tanstack/vue-query';
|
||||||
|
import UserTimezoneMismatchModal from './UserTimezoneMismatchModal.vue';
|
||||||
|
import TimezoneMismatchModal from '@/packages/ui/src/TimezoneMismatchModal.vue';
|
||||||
|
import { api } from '@/packages/api/src';
|
||||||
|
|
||||||
|
vi.mock('@/packages/api/src', async (importOriginal) => ({
|
||||||
|
...(await importOriginal<typeof import('@/packages/api/src')>()),
|
||||||
|
api: {
|
||||||
|
updateUser: vi.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('@inertiajs/vue3', async (importOriginal) => ({
|
||||||
|
...(await importOriginal<typeof import('@inertiajs/vue3')>()),
|
||||||
|
usePage: () => ({
|
||||||
|
props: {
|
||||||
|
auth: {
|
||||||
|
user: { id: 'user-1' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
function mountModal() {
|
||||||
|
return shallowMount(UserTimezoneMismatchModal, {
|
||||||
|
props: { show: true },
|
||||||
|
global: {
|
||||||
|
plugins: [createPinia(), VueQueryPlugin],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('UserTimezoneMismatchModal', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.mocked(api.updateUser).mockReset();
|
||||||
|
Object.defineProperty(window.location, 'reload', {
|
||||||
|
configurable: true,
|
||||||
|
value: vi.fn(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('saves the new timezone through the users API', async () => {
|
||||||
|
vi.mocked(api.updateUser).mockResolvedValue({
|
||||||
|
data: { id: 'user-1', timezone: 'Australia/Sydney' },
|
||||||
|
} as never);
|
||||||
|
|
||||||
|
const wrapper = mountModal();
|
||||||
|
wrapper.findComponent(TimezoneMismatchModal).vm.$emit('update', 'Australia/Sydney');
|
||||||
|
await flushPromises();
|
||||||
|
|
||||||
|
expect(api.updateUser).toHaveBeenCalledWith(
|
||||||
|
{ timezone: 'Australia/Sydney' },
|
||||||
|
{ params: { user: 'user-1' } }
|
||||||
|
);
|
||||||
|
expect(wrapper.findComponent(TimezoneMismatchModal).props('show')).toBe(false);
|
||||||
|
expect(window.location.reload).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('leaves the modal open when the update fails', async () => {
|
||||||
|
vi.mocked(api.updateUser).mockRejectedValue(new Error('nope'));
|
||||||
|
|
||||||
|
const wrapper = mountModal();
|
||||||
|
wrapper.findComponent(TimezoneMismatchModal).vm.$emit('update', 'Australia/Sydney');
|
||||||
|
await flushPromises();
|
||||||
|
|
||||||
|
expect(wrapper.findComponent(TimezoneMismatchModal).props('show')).toBe(true);
|
||||||
|
expect(window.location.reload).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue';
|
import { usePage } from '@inertiajs/vue3';
|
||||||
import { useForm, usePage } from '@inertiajs/vue3';
|
|
||||||
import type { User } from '@/types/models';
|
import type { User } from '@/types/models';
|
||||||
import TimezoneMismatchModal from '@/packages/ui/src/TimezoneMismatchModal.vue';
|
import TimezoneMismatchModal from '@/packages/ui/src/TimezoneMismatchModal.vue';
|
||||||
|
import { useUpdateUserMutation } from '@/utils/useUserQuery';
|
||||||
|
|
||||||
const show = defineModel('show', { default: false });
|
const show = defineModel('show', { default: false });
|
||||||
const saving = ref(false);
|
|
||||||
|
|
||||||
const page = usePage<{
|
const page = usePage<{
|
||||||
auth: {
|
auth: {
|
||||||
@@ -13,33 +12,28 @@ const page = usePage<{
|
|||||||
};
|
};
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
function handleUpdate(timezone: string) {
|
const updateUser = useUpdateUserMutation();
|
||||||
saving.value = true;
|
|
||||||
const form = useForm({
|
|
||||||
_method: 'PUT',
|
|
||||||
timezone: timezone,
|
|
||||||
name: page.props.auth.user.name,
|
|
||||||
email: page.props.auth.user.email,
|
|
||||||
week_start: page.props.auth.user.week_start,
|
|
||||||
});
|
|
||||||
|
|
||||||
form.post(route('user-profile-information.update'), {
|
async function handleUpdate(timezone: string) {
|
||||||
errorBag: 'updateProfileInformation',
|
try {
|
||||||
preserveScroll: true,
|
await updateUser.mutateAsync({
|
||||||
onSuccess: () => {
|
userId: page.props.auth.user.id,
|
||||||
saving.value = false;
|
body: { timezone },
|
||||||
show.value = false;
|
});
|
||||||
location.reload();
|
show.value = false;
|
||||||
},
|
// reload the whole page to re-read the timezone update
|
||||||
onError: () => {
|
location.reload();
|
||||||
saving.value = false;
|
} catch {
|
||||||
},
|
// notification handled by mutation
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<TimezoneMismatchModal v-model:show="show" :saving="saving" @update="handleUpdate" />
|
<TimezoneMismatchModal
|
||||||
|
v-model:show="show"
|
||||||
|
:saving="updateUser.isPending.value"
|
||||||
|
@update="handleUpdate" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped></style>
|
<style scoped></style>
|
||||||
|
|||||||
Reference in New Issue
Block a user