-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moved methods to the right folder, added more interfaces
- Loading branch information
Showing
11 changed files
with
259 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface UserAutocomplete { | ||
label: string; | ||
value: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface UserData { | ||
userId: string; | ||
login: string; | ||
displayName: string; | ||
email: string; | ||
status: string; | ||
lastModified: string; | ||
authSource: string; | ||
twoFactorEnabled: string; | ||
isSuperadmin: string; | ||
json(): Promise<UserData>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { describe, expect, test, vi, beforeEach } from 'vitest'; | ||
import autocomplete from 'autocompleter'; | ||
import { updateUser } from './users'; | ||
import { fetchUsers } from '../api'; | ||
import { addElement } from '../../../../assets/src/utils'; | ||
import './autocomplete'; // Ensure the event listener is registered | ||
|
||
vi.mock('autocompleter', () => ({ | ||
__esModule: true, | ||
default: vi.fn(), | ||
})); | ||
|
||
vi.mock('./users', () => ({ | ||
updateUser: vi.fn(), | ||
})); | ||
|
||
vi.mock('../api', () => ({ | ||
fetchUsers: vi.fn(), | ||
})); | ||
|
||
vi.mock('../../../../assets/src/utils', () => ({ | ||
addElement: vi.fn(() => document.createElement('div')), | ||
})); | ||
|
||
describe('User Autocomplete', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = ` | ||
<input id="pmf-user-list-autocomplete" /> | ||
`; | ||
}); | ||
|
||
test('should initialize autocomplete on DOMContentLoaded', () => { | ||
const mockAutocomplete = vi.fn(); | ||
(autocomplete as unknown as vi.Mock).mockImplementation(mockAutocomplete); | ||
|
||
document.dispatchEvent(new Event('DOMContentLoaded')); | ||
|
||
expect(mockAutocomplete).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should call updateUser on item select', async () => { | ||
const mockItem = { label: 'John Doe', value: '1' }; | ||
const input = document.getElementById('pmf-user-list-autocomplete') as HTMLInputElement; | ||
|
||
const onSelect = (autocomplete as unknown as vi.Mock).mock.calls[0][0].onSelect; | ||
await onSelect(mockItem, input); | ||
|
||
expect(updateUser).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
test('should fetch and filter users', async () => { | ||
const mockUsers = [{ label: 'John Doe', value: '1' }]; | ||
(fetchUsers as unknown as vi.Mock).mockResolvedValue(mockUsers); | ||
|
||
const fetch = (autocomplete as unknown as vi.Mock).mock.calls[0][0].fetch; | ||
const callback = vi.fn(); | ||
await fetch('john', callback); | ||
|
||
expect(fetchUsers).toHaveBeenCalledWith('john'); | ||
expect(callback).toHaveBeenCalledWith(mockUsers); | ||
}); | ||
|
||
test('should render user suggestions', () => { | ||
const mockItem = { label: 'John Doe', value: '1' }; | ||
const render = (autocomplete as unknown as vi.Mock).mock.calls[0][0].render; | ||
const result = render(mockItem, 'john'); | ||
|
||
expect(addElement).toHaveBeenCalledWith('div', { | ||
classList: 'pmf-user-list-result border', | ||
innerHTML: '<strong>John</strong> Doe', | ||
}); | ||
expect(result).toBeInstanceOf(HTMLDivElement); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import { activateUser, deleteUser, overwritePassword, postUserData } from '../api'; | ||
|
||
global.fetch = vi.fn(); | ||
|
||
describe('User API', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should overwrite password', async () => { | ||
const mockResponse = { success: true }; | ||
(fetch as vi.Mock).mockResolvedValue({ | ||
json: vi.fn().mockResolvedValue(mockResponse), | ||
}); | ||
|
||
const response = await overwritePassword('csrfToken', 'userId', 'newPassword', 'passwordRepeat'); | ||
expect(fetch).toHaveBeenCalledWith('./api/user/overwrite-password', expect.any(Object)); | ||
expect(response).toEqual(mockResponse); | ||
}); | ||
|
||
it('should post user data', async () => { | ||
const mockResponse = { success: true }; | ||
(fetch as vi.Mock).mockResolvedValue({ | ||
json: vi.fn().mockResolvedValue(mockResponse), | ||
}); | ||
|
||
const response = await postUserData('url', { key: 'value' }); | ||
expect(fetch).toHaveBeenCalledWith('url', expect.any(Object)); | ||
expect(response).toEqual(mockResponse); | ||
}); | ||
|
||
it('should activate user', async () => { | ||
const mockResponse = { success: true }; | ||
(fetch as vi.Mock).mockResolvedValue({ | ||
json: vi.fn().mockResolvedValue(mockResponse), | ||
}); | ||
|
||
const response = await activateUser('userId', 'csrfToken'); | ||
expect(fetch).toHaveBeenCalledWith('./api/user/activate', expect.any(Object)); | ||
expect(response).toEqual(mockResponse); | ||
}); | ||
|
||
it('should delete user', async () => { | ||
const mockResponse = { success: true }; | ||
(fetch as vi.Mock).mockResolvedValue({ | ||
json: vi.fn().mockResolvedValue(mockResponse), | ||
}); | ||
|
||
const response = await deleteUser('userId', 'csrfToken'); | ||
expect(fetch).toHaveBeenCalledWith('./api/user/delete', expect.any(Object)); | ||
expect(response).toEqual(mockResponse); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.