Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ba8e493

Browse filesBrowse files
committed
feature(tests) add email validation tests
1 parent 7523923 commit ba8e493
Copy full SHA for ba8e493

File tree

Expand file treeCollapse file tree

5 files changed

+79
-29
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+79
-29
lines changed

‎components/content/Alert.vue

Copy file name to clipboardExpand all lines: components/content/Alert.vue
+11-14Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
:component="icon"
77
class="inline-flex mr-2 w-5 h-5 justify-center items-center text-1.2rem"
88
>
9-
{{ icon }}
10-
9+
<!-- {{ icon }} -->
10+
<!-- <Icon class="w-5 h-5 text-yellow-400" aria-hidden="true" :name="icon" /> -->
11+
<Icon class="w-5 h-5 text-yellow-400" aria-hidden="true" name="carbon:ai-status-complete" />
1112
</div>
1213
<slot/>
1314
<div class="flex-grow alert-content">
@@ -20,23 +21,19 @@
2021
</div>
2122
</div>
2223
</template>
23-
24-
<script>
25-
26-
export default defineComponent({
27-
props: {
28-
icon: {
29-
type: String,
30-
default: ''
31-
},
32-
type: {
24+
<script setup lang="ts">
25+
defineProps({
26+
icon: {
27+
type: String,
28+
default: 'heroicons-outline:exclamation'
29+
},
30+
type: {
3331
type: String,
3432
default: 'info',
35-
validator (value) {
33+
validator (value: string) {
3634
return ['info', 'success', 'warning', 'danger'].includes(value)
3735
}
3836
}
39-
}
4037
})
4138
</script>
4239

‎content/tutorials/state-in-nuxt3.md

Copy file name to clipboardExpand all lines: content/tutorials/state-in-nuxt3.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Every time we visit or refresh **localhost:3000/api/counter** the count will in
2525
You may be tempted to believe that if the count survived a browser refresh, it must must persistent, right?
2626
We must keep in mind, the browser is not the only thing that can restart. The server can as well.
2727

28-
::alert{background=bg-purple-800 icon=🚨}
28+
::alert{type=info icon=🚨}
2929
Don't let this fool you into thinking this is persistent state. As soon as you restart the server, the count will be back to 0!
3030
::
3131

‎package.json

Copy file name to clipboardExpand all lines: package.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"scripts": {
44
"build": "nuxt build",
5-
"dev": "nuxt dev",
5+
"dev": "nuxt dev --trace-warnings",
66
"generate": "nuxt generate",
77
"preview": "nuxt preview",
88
"test": "vitest",

‎server/services/validator.ts

Copy file name to clipboardExpand all lines: server/services/validator.ts
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { RegistationRequest } from '~~/types/IRegistration';
22
import { getUserByEmail, getUserByUserName } from '~/server/database/repositories/userRespository';
33

4-
54
export async function validate(data: RegistationRequest) {
65

76
const errors = new Map<string, { check: InputValidation }>()
@@ -46,7 +45,7 @@ async function runChecks(key: string, value: string): Promise<InputValidation> {
4645
const isValidEmail = validateEmail(value)
4746

4847
if (!isValidEmail) {
49-
check.emailTaken = true
48+
check.emailTaken = null
5049
check.hasError = true
5150
check.errorMessage = `${value}, is not a valid email!`
5251
return check

‎tests/registar.test.ts

Copy file name to clipboard
+65-11Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,67 @@
1-
import { describe, it, expect } from 'vitest'
2-
import { fileURLToPath } from 'node:url'
3-
import { setup, $fetch } from '@nuxt/test-utils-edge'
4-
describe('ssr', async () => {
5-
await setup({
6-
rootDir: fileURLToPath(new URL('./fixture', import.meta.url)),
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { validate } from '../server/services/validator'
3+
4+
5+
describe('test email validation', async () => {
6+
it('should return error if email missing', async () => {
7+
const res = await validate({
8+
username: '',
9+
name: '',
10+
email: null,
11+
password: '1234567'
12+
})
13+
14+
const emailVal = res.get('email')
15+
16+
emailVal.check.errorMessage
17+
18+
expect(res.has('email')).toBe(true)
19+
expect(emailVal.check.hasError).toBe(true)
20+
expect(emailVal.check.errorMessage).toContain('email is required')
721
})
8-
it('renders the index page', async () => {
9-
// Get response to a server-rendered page with `$fetch`.
10-
const html = await $fetch('/')
11-
expect(html).toContain('Full Stack')
22+
23+
24+
it('should return error for improperly formatted email', async () => {
25+
const res = await validate({
26+
username: '',
27+
name: '',
28+
email: 'test',
29+
password: '1234567'
30+
})
31+
32+
const emailVal = res.get('email')
33+
34+
emailVal.check.errorMessage
35+
36+
expect(res.has('email')).toBe(true)
37+
expect(emailVal.check.hasError).toBe(true)
38+
expect(emailVal.check.errorMessage).toContain('test, is not a valid email!')
1239
})
13-
})
40+
41+
it('should return error if email taken', async () => {
42+
43+
vi.mock('~/server/database/repositories/userRespository', () => {
44+
return {
45+
getUserByEmail: vi.fn(() => ({ email: 'test@fullstackjack.com' }))
46+
}
47+
})
48+
49+
const email = 'test@fullstackjack.com'
50+
51+
const res = await validate({
52+
username: '',
53+
name: '',
54+
email: email,
55+
password: '1234567'
56+
})
57+
58+
const emailVal = res.get('email')
59+
60+
emailVal.check.errorMessage
61+
62+
expect(res.has('email')).toBe(true)
63+
expect(emailVal.check.hasError).toBe(true)
64+
expect(emailVal.check.emailTaken).toBe(true)
65+
expect(emailVal.check.errorMessage).toContain(`This email, test@fullstackjack.com, is already registered!`)
66+
})
67+
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.