-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfc.spec.ts
More file actions
98 lines (75 loc) · 3.05 KB
/
nfc.spec.ts
File metadata and controls
98 lines (75 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ERROR_CODES, METHODS, STATUS } from '../../types'
describe('devices:nfc', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.resetModules()
})
describe('readTag', () => {
it('success response', async () => {
const response = { ref: 'nfc-read', payload: { status: STATUS.SUCCESS, nfcTag: { id: 'tag1' } } }
const sendClientEventMock = vi.fn().mockResolvedValue(response)
vi.doMock('@expressms/smartapp-bridge', () => ({
default: { sendClientEvent: sendClientEventMock },
}))
const { readTag } = await import('./nfc')
const result = await readTag()
expect(sendClientEventMock).toHaveBeenCalledTimes(1)
expect(sendClientEventMock).toHaveBeenCalledWith({
method: METHODS.READ_NFC_TAG,
params: {},
})
expect(result).toBe(response)
})
it('no bridge', async () => {
vi.doMock('@expressms/smartapp-bridge', () => ({ default: undefined }))
const { readTag } = await import('./nfc')
await expect(readTag()).rejects.toBe(ERROR_CODES.NO_BRIDGE)
})
})
describe('writeTag', () => {
it('success response', async () => {
const response = { ref: 'nfc-write', payload: { status: STATUS.SUCCESS } }
const sendClientEventMock = vi.fn().mockResolvedValue(response)
vi.doMock('@expressms/smartapp-bridge', () => ({
default: { sendClientEvent: sendClientEventMock },
}))
const { writeTag } = await import('./nfc')
const messages = [{ recordType: 'text', data: 'hello' } as unknown as unknown]
const result = await writeTag(messages as unknown as [])
expect(sendClientEventMock).toHaveBeenCalledTimes(1)
expect(sendClientEventMock).toHaveBeenCalledWith({
method: METHODS.WRITE_NFC_TAG,
params: { messages },
})
expect(result).toBe(response)
})
it('no bridge', async () => {
vi.doMock('@expressms/smartapp-bridge', () => ({ default: undefined }))
const { writeTag } = await import('./nfc')
await expect(writeTag([] as never)).rejects.toBe(ERROR_CODES.NO_BRIDGE)
})
})
describe('getStatus', () => {
it('success response', async () => {
const response = { ref: 'nfc-status', payload: { status: STATUS.SUCCESS, nfcEnabled: true } }
const sendClientEventMock = vi.fn().mockResolvedValue(response)
vi.doMock('@expressms/smartapp-bridge', () => ({
default: { sendClientEvent: sendClientEventMock },
}))
const { getStatus } = await import('./nfc')
const result = await getStatus()
expect(sendClientEventMock).toHaveBeenCalledTimes(1)
expect(sendClientEventMock).toHaveBeenCalledWith({
method: METHODS.GET_NFC_STATUS,
params: {},
})
expect(result).toBe(response)
})
it('no bridge', async () => {
vi.doMock('@expressms/smartapp-bridge', () => ({ default: undefined }))
const { getStatus } = await import('./nfc')
await expect(getStatus()).rejects.toBe(ERROR_CODES.NO_BRIDGE)
})
})
})