zhou zhou
12 小时以前 40905cbd04c2e332cd4bc2b9e0c5b3e1da9cccfa
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import assert from 'node:assert/strict'
import test from 'node:test'
 
import {
  buildUserDialogModel,
  buildUserPageQueryParams,
  buildUserSavePayload,
  buildUserSearchParams,
  getUserStatusMeta,
  mergeUserDetailRecord,
  normalizeDeptTreeOptions,
  normalizeRoleOptions,
  normalizeUserListRow
} from '../src/views/system/user/userPage.helpers.js'
 
test('buildUserSearchParams keeps real user page search fields', () => {
  assert.deepEqual(
    buildUserSearchParams({
      username: 'root',
      nickname: '管理员',
      phone: '13800000000',
      email: 'root@example.com',
      status: 1,
      deptId: 0,
      roleIds: [3, 8],
      code: 'A001',
      sex: 1,
      realName: 'Vincent',
      idCard: '330421199511233211',
      condition: 'root'
    }),
    {
      username: 'root',
      nickname: '管理员',
      phone: '13800000000',
      email: 'root@example.com',
      status: 1,
      deptId: 0,
      roleIds: [3, 8],
      code: 'A001',
      sex: 1,
      realName: 'Vincent',
      idCard: '330421199511233211',
      condition: 'root'
    }
  )
})
 
test('buildUserPageQueryParams merges paging and search fields', () => {
  assert.deepEqual(
    buildUserPageQueryParams({
      current: 2,
      size: 20,
      username: 'root',
      condition: 'manager'
    }),
    {
      current: 2,
      pageSize: 20,
      username: 'root',
      condition: 'manager'
    }
  )
})
 
test('buildUserDialogModel normalizes backend edit data into the form model', () => {
  assert.deepEqual(
    buildUserDialogModel({
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      roles: [{ id: 3 }, { roleId: 8 }],
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 0
    }),
    {
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      roleIds: [3, 8],
      userRoleIds: [3, 8],
      password: '',
      confirmPassword: '',
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 0
    }
  )
})
 
test('buildUserSavePayload submits roleIds and password for the backend', () => {
  assert.deepEqual(
    buildUserSavePayload({
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      userRoleIds: [3, 8],
      password: 'secret',
      confirmPassword: 'secret',
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 1
    }),
    {
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptId: 1,
      roleIds: [3, 8],
      password: 'secret',
      sex: 1,
      code: 'A001',
      phone: '13800000000',
      email: 'root@example.com',
      realName: 'Vincent',
      idCard: '330421199511233211',
      memo: 'memo',
      status: 1
    }
  )
})
 
test('normalizeUserListRow exposes table friendly fields', () => {
  const normalized = normalizeUserListRow({
    username: 'root',
    nickname: '管理员',
    deptLabel: '研发部',
    deptId$: '研发部',
    roleNames: '超级管理员、OPS',
    roles: [{ name: '超级管理员' }, { code: 'OPS' }],
    status: 1,
    createTime$: '2025-03-28 10:00:00',
    updateTime: '2025-03-28 11:00:00'
  })
 
  assert.equal(normalized.username, 'root')
  assert.equal(normalized.deptLabel, '研发部')
  assert.equal(normalized.roleNames, '超级管理员、OPS')
  assert.equal(normalized.statusBool, true)
  assert.equal(normalized.statusText, '正常')
  assert.equal(normalized.statusType, 'success')
  assert.equal(normalized.createTimeText, '2025-03-28 10:00:00')
  assert.equal(normalized.updateTimeText, '2025-03-28 11:00:00')
})
 
test('mergeUserDetailRecord keeps list row roles when detail omits them', () => {
  assert.deepEqual(
    mergeUserDetailRecord(
      {
        id: 7,
        username: 'root',
        nickname: '管理员'
      },
      {
        id: 7,
        deptLabel: '研发部',
        roleNames: '超级管理员',
        roles: [{ id: 3, name: '超级管理员' }]
      }
    ),
    {
      id: 7,
      username: 'root',
      nickname: '管理员',
      deptLabel: '研发部',
      roleNames: '超级管理员',
      roles: [{ id: 3, name: '超级管理员' }]
    }
  )
})
 
test('normalizeDeptTreeOptions and normalizeRoleOptions adapt lookup data', () => {
  assert.deepEqual(
    normalizeDeptTreeOptions([{ id: 1, name: '总部', children: [{ id: 2, name: '研发部' }] }]),
    [{ value: 1, label: '总部', children: [{ value: 2, label: '研发部', children: [] }] }]
  )
 
  assert.deepEqual(
    normalizeRoleOptions([{ id: 3, name: '管理员' }, { roleId: 8, code: 'OPS' }]),
    [
      { value: 3, label: '管理员' },
      { value: 8, label: 'OPS' }
    ]
  )
})
 
test('getUserStatusMeta maps enabled and disabled states', () => {
  assert.deepEqual(getUserStatusMeta(1), { type: 'success', text: '正常', bool: true })
  assert.deepEqual(getUserStatusMeta(0), { type: 'danger', text: '禁用', bool: false })
})