Skip to content

Commit 6b0e1ad

Browse files
committed
Rework test around the test failure
1 parent 74758c4 commit 6b0e1ad

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed

packages/sprinkle-account/app/assets/composables/useRegisterApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function useRegisterApi() {
2929
}
3030
}
3131

32-
function availableLocales(): string[] {
32+
function availableLocales(): Record<string, string> {
3333
return useConfigStore().get('locales.available')
3434
}
3535

packages/sprinkle-account/app/assets/tests/composables/useRegisterApi.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useConfigStore } from '@userfrosting/sprinkle-core/stores'
66
import type { RegisterRequest } from '../../interfaces'
77
import { useRegisterApi } from '../../composables'
88

9-
const { submitRegistration, defaultRegistrationForm, availableLocales, captchaUrl } =
9+
const { submitRegistration, defaultRegistrationForm, availableLocales, captchaUrl, apiLoading } =
1010
useRegisterApi()
1111

1212
const testUser: UserInterface = {
@@ -122,4 +122,16 @@ describe('register', () => {
122122
})
123123
expect(axios.post).toHaveBeenCalledWith('/account/register', form)
124124
})
125+
126+
test('should set loading state to true', async () => {
127+
// Arrange
128+
vi.spyOn(axios, 'post').mockResolvedValue({ data: testUser } as any)
129+
130+
// Act
131+
expect(apiLoading.value).toBe(false)
132+
const submitPromise = submitRegistration(form)
133+
expect(apiLoading.value).toBe(true)
134+
await submitPromise
135+
expect(apiLoading.value).toBe(false)
136+
})
125137
})

packages/theme-pink-cupcake/src/components/Pages/Account/FormRegister.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ const tos = computed(() => {
162162
<div class="uk-width-2-3">
163163
<input
164164
class="uk-input"
165-
type="password"
165+
type="text"
166166
:placeholder="$t('CAPTCHA.SPECIFY')"
167167
aria-label="Captcha"
168168
id="r-form-captcha"
@@ -180,7 +180,10 @@ const tos = computed(() => {
180180
<p v-html="tos"></p>
181181

182182
<div class="uk-text-center">
183-
<button class="uk-button uk-button-primary" :disabled="apiLoading ? true : false">
183+
<button
184+
class="uk-button uk-button-primary"
185+
:disabled="apiLoading ? true : false"
186+
data-test="submit">
184187
{{ $t('REGISTER_ME') }}
185188
</button>
186189
</div>

packages/theme-pink-cupcake/src/tests/components/Content/FormRegister.test.ts

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { ref } from 'vue'
12
import { mount, config } from '@vue/test-utils'
23
import { describe, test, expect, vi, afterEach } from 'vitest'
34
import UIkit from 'uikit'
45
import type { UserInterface, RegisterRequest } from '@userfrosting/sprinkle-account/interfaces'
6+
import { useRegisterApi } from '@userfrosting/sprinkle-account/composables'
57
import type { AlertInterface } from '@userfrosting/sprinkle-core/interfaces'
68
import FormRegister from '../../../components/Pages/Account/FormRegister.vue'
79
import UFAlert from '../../../components/UFAlert.vue'
@@ -68,17 +70,6 @@ const uikitNotification = {
6870
timeout: 4000
6971
}
7072

71-
// Mock the register composable
72-
const mockedSubmitRegistration = vi.fn()
73-
vi.mock('@userfrosting/sprinkle-account/composables', () => ({
74-
useRegisterApi: () => ({
75-
defaultRegistrationForm: vi.fn(() => defaultForm),
76-
availableLocales: vi.fn(() => availableLocales),
77-
captchaUrl: vi.fn(() => '/account/captcha'),
78-
submitRegistration: mockedSubmitRegistration
79-
})
80-
}))
81-
8273
// Mock the config & translator store
8374
vi.mock('@userfrosting/sprinkle-core/stores', () => ({
8475
useConfigStore: () => ({
@@ -97,21 +88,42 @@ vi.mock('vue-router', () => ({
9788
})
9889
}))
9990

91+
vi.mock('@userfrosting/sprinkle-account/composables', () => ({
92+
useRegisterApi: vi.fn()
93+
}))
94+
10095
describe('FormRegister.vue', () => {
10196
afterEach(() => {
10297
vi.clearAllMocks()
10398
})
10499

105100
test('renders correctly', () => {
101+
vi.mocked(useRegisterApi).mockReturnValue({
102+
submitRegistration: vi.fn(),
103+
defaultRegistrationForm: vi.fn(() => defaultForm),
104+
availableLocales: vi.fn(() => availableLocales),
105+
captchaUrl: vi.fn(() => '/account/captcha'),
106+
apiLoading: ref(false),
107+
apiError: ref(null)
108+
})
109+
106110
const wrapper = mount(FormRegister)
107111
expect(wrapper.exists()).toBe(true)
108112
})
109113

110114
test('handles successful register', async () => {
111-
vi.mocked(mockedSubmitRegistration).mockResolvedValueOnce({
115+
const mockedSubmitRegistration = vi.fn().mockResolvedValueOnce({
112116
user: testUser,
113117
message: 'Succesfully registered John Doe!'
114118
})
119+
vi.mocked(useRegisterApi).mockReturnValue({
120+
submitRegistration: mockedSubmitRegistration,
121+
defaultRegistrationForm: vi.fn(() => defaultForm),
122+
availableLocales: vi.fn(() => availableLocales),
123+
captchaUrl: vi.fn(() => '/account/captcha'),
124+
apiLoading: ref(true),
125+
apiError: ref(null)
126+
})
115127
vi.spyOn(UIkit, 'notification')
116128

117129
const wrapper = mount(FormRegister)
@@ -126,29 +138,59 @@ describe('FormRegister.vue', () => {
126138
expect(UIkit.notification).toHaveBeenCalledWith(uikitNotification)
127139
})
128140

129-
test('handles registration failure', async () => {
130-
const mockError: AlertInterface = { title: 'Invalid credentials' }
131-
vi.mocked(mockedSubmitRegistration).mockRejectedValueOnce(mockError)
132-
vi.spyOn(UIkit, 'notification')
141+
test('disables submit button when loading', () => {
142+
vi.mocked(useRegisterApi).mockReturnValue({
143+
submitRegistration: vi.fn(),
144+
defaultRegistrationForm: vi.fn(() => defaultForm),
145+
availableLocales: vi.fn(() => availableLocales),
146+
captchaUrl: vi.fn(() => '/account/captcha'),
147+
apiLoading: ref(true), // Set loading state to true
148+
apiError: ref(null)
149+
})
133150

134151
const wrapper = mount(FormRegister)
135-
// @ts-ignore
136-
wrapper.vm.formData = testForm
137-
await (wrapper.vm as any).submitForm()
152+
expect(wrapper.find('[data-test="submit"]').exists()).toBe(true)
153+
expect(wrapper.find('[data-test="submit"]').text()).toBe('REGISTER_ME')
154+
expect(wrapper.find('[data-test="submit"]').attributes().disabled).toBeDefined()
155+
})
156+
157+
test('handles registration failure', async () => {
158+
const mockedApiError: AlertInterface = {
159+
title: 'Registration error',
160+
description: 'You did not enter the captcha code correctly.',
161+
style: 'Danger',
162+
closeBtn: true
163+
}
164+
vi.mocked(useRegisterApi).mockReturnValue({
165+
submitRegistration: vi.fn(),
166+
defaultRegistrationForm: vi.fn(() => defaultForm),
167+
availableLocales: vi.fn(() => availableLocales),
168+
captchaUrl: vi.fn(() => '/account/captcha'),
169+
apiLoading: ref(true),
170+
apiError: ref(mockedApiError)
171+
})
172+
const wrapper = mount(FormRegister)
138173

139174
// Spy on the authStore & UIkit notification method
140-
expect(mockedSubmitRegistration).toHaveBeenCalledTimes(1)
141-
expect(mockedSubmitRegistration).toHaveBeenCalledWith(testForm)
142-
expect(UIkit.notification).not.toHaveBeenCalled()
143175
expect(wrapper.find('[data-test="error"]').exists()).toBe(true)
144-
expect(wrapper.get('[data-test="error"]').text()).toMatch('Invalid credentials')
176+
expect(wrapper.get('[data-test="error"]').text()).toMatch(
177+
'Registration error You did not enter the captcha code correctly.'
178+
)
145179
})
146180

147181
test('Handle form using the v-model', async () => {
148-
vi.mocked(mockedSubmitRegistration).mockResolvedValueOnce({
182+
const mockedSubmitRegistration = vi.fn().mockResolvedValueOnce({
149183
user: testUser,
150184
message: 'Succesfully registered John Doe!'
151185
})
186+
vi.mocked(useRegisterApi).mockReturnValue({
187+
submitRegistration: mockedSubmitRegistration,
188+
defaultRegistrationForm: vi.fn(() => defaultForm),
189+
availableLocales: vi.fn(() => availableLocales),
190+
captchaUrl: vi.fn(() => '/account/captcha'),
191+
apiLoading: ref(true),
192+
apiError: ref(null)
193+
})
152194
vi.spyOn(UIkit, 'notification')
153195

154196
const wrapper = mount(FormRegister)

0 commit comments

Comments
 (0)