-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tests for admin panel logs page (#183)
- Loading branch information
Showing
4 changed files
with
246 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { | ||
describe, it, expect, vi, beforeEach, Mock, | ||
} from 'vitest' | ||
import { screen } from '@testing-library/react' | ||
import { render } from '../../../vitest.setup' | ||
import Page from 'app/[lang]/logs/page' | ||
import { | ||
useGetApiV1LogsEmailQuery, | ||
useGetApiV1LogsSignInQuery, | ||
useGetApiV1LogsSmsQuery, | ||
} from 'services/auth/api' | ||
import { | ||
emailLogs, signInLogs, smsLogs, | ||
} from 'tests/logMock' | ||
import { configSignal } from 'signals' | ||
|
||
vi.mock( | ||
'services/auth/api', | ||
() => ({ | ||
useGetApiV1LogsEmailQuery: vi.fn(), | ||
useGetApiV1LogsSignInQuery: vi.fn(), | ||
useGetApiV1LogsSmsQuery: vi.fn(), | ||
}), | ||
) | ||
|
||
vi.mock( | ||
'signals', | ||
() => ({ | ||
configSignal: { | ||
value: { | ||
ENABLE_EMAIL_LOG: true, | ||
ENABLE_SMS_LOG: true, | ||
ENABLE_SIGN_IN_LOG: true, | ||
}, | ||
subscribe: () => () => {}, | ||
}, | ||
}), | ||
) | ||
|
||
describe( | ||
'Page Component', | ||
() => { | ||
beforeEach(() => { | ||
(useGetApiV1LogsEmailQuery as Mock).mockReturnValue({ data: { logs: emailLogs } }); | ||
(useGetApiV1LogsSmsQuery as Mock).mockReturnValue({ data: { logs: smsLogs } }); | ||
(useGetApiV1LogsSignInQuery as Mock).mockReturnValue({ data: { logs: signInLogs } }) | ||
}) | ||
|
||
it( | ||
'render logs', | ||
async () => { | ||
render(<Page />) | ||
|
||
const emailRows = screen.queryAllByTestId('emailLogRow') | ||
expect(emailRows.length).toBe(emailLogs.length) | ||
emailRows.forEach(( | ||
row, index, | ||
) => { | ||
expect(row.querySelectorAll('td')[0]?.innerHTML).toContain(emailLogs[index].receiver) | ||
const editLink = row.querySelectorAll('td')[3]?.getElementsByTagName('a') | ||
expect(editLink[0].getAttribute('href')).toBe(`/en/logs/email/${emailLogs[index].id}`) | ||
}) | ||
|
||
const smsRows = screen.queryAllByTestId('smsLogRow') | ||
expect(smsRows.length).toBe(smsLogs.length) | ||
smsRows.forEach(( | ||
row, index, | ||
) => { | ||
expect(row.querySelectorAll('td')[0]?.innerHTML).toContain(smsLogs[index].receiver) | ||
const editLink = row.querySelectorAll('td')[3]?.getElementsByTagName('a') | ||
expect(editLink[0].getAttribute('href')).toBe(`/en/logs/sms/${smsLogs[index].id}`) | ||
}) | ||
|
||
const signInRows = screen.queryAllByTestId('signInRow') | ||
expect(signInRows.length).toBe(signInLogs.length) | ||
signInRows.forEach(( | ||
row, index, | ||
) => { | ||
expect(row.querySelectorAll('td')[0]?.innerHTML).toContain(signInLogs[index].userId) | ||
const editLink = row.querySelectorAll('td')[2]?.getElementsByTagName('a') | ||
expect(editLink[0].getAttribute('href')).toBe(`/en/logs/sign-in/${signInLogs[index].userId}`) | ||
}) | ||
}, | ||
) | ||
|
||
it( | ||
'suppress email logs', | ||
async () => { | ||
vi.mocked(configSignal as unknown as { value: object }).value = { | ||
ENABLE_EMAIL_LOG: false, | ||
ENABLE_SMS_LOG: true, | ||
ENABLE_SIGN_IN_LOG: true, | ||
} | ||
|
||
render(<Page />) | ||
|
||
const emailRows = screen.queryAllByTestId('emailLogRow') | ||
expect(emailRows.length).toBe(0) | ||
|
||
const smsRows = screen.queryAllByTestId('smsLogRow') | ||
expect(smsRows.length).toBe(smsLogs.length) | ||
|
||
const signInRows = screen.queryAllByTestId('signInRow') | ||
expect(signInRows.length).toBe(signInLogs.length) | ||
|
||
vi.mocked(configSignal as unknown as { value: object }).value = { | ||
ENABLE_EMAIL_LOG: true, | ||
ENABLE_SMS_LOG: true, | ||
ENABLE_SIGN_IN_LOG: true, | ||
} | ||
}, | ||
) | ||
|
||
it( | ||
'suppress sms logs', | ||
async () => { | ||
vi.mocked(configSignal as unknown as { value: object }).value = { | ||
ENABLE_EMAIL_LOG: true, | ||
ENABLE_SMS_LOG: false, | ||
ENABLE_SIGN_IN_LOG: true, | ||
} | ||
|
||
render(<Page />) | ||
|
||
const emailRows = screen.queryAllByTestId('emailLogRow') | ||
expect(emailRows.length).toBe(2) | ||
|
||
const smsRows = screen.queryAllByTestId('smsLogRow') | ||
expect(smsRows.length).toBe(0) | ||
|
||
const signInRows = screen.queryAllByTestId('signInRow') | ||
expect(signInRows.length).toBe(signInLogs.length) | ||
|
||
vi.mocked(configSignal as unknown as { value: object }).value = { | ||
ENABLE_EMAIL_LOG: true, | ||
ENABLE_SMS_LOG: true, | ||
ENABLE_SIGN_IN_LOG: true, | ||
} | ||
}, | ||
) | ||
|
||
it( | ||
'suppress signIn logs', | ||
async () => { | ||
vi.mocked(configSignal as unknown as { value: object }).value = { | ||
ENABLE_EMAIL_LOG: true, | ||
ENABLE_SMS_LOG: true, | ||
ENABLE_SIGN_IN_LOG: false, | ||
} | ||
|
||
render(<Page />) | ||
|
||
const emailRows = screen.queryAllByTestId('emailLogRow') | ||
expect(emailRows.length).toBe(2) | ||
|
||
const smsRows = screen.queryAllByTestId('smsLogRow') | ||
expect(smsRows.length).toBe(2) | ||
|
||
const signInRows = screen.queryAllByTestId('signInRow') | ||
expect(signInRows.length).toBe(0) | ||
|
||
vi.mocked(configSignal as unknown as { value: object }).value = { | ||
ENABLE_EMAIL_LOG: true, | ||
ENABLE_SMS_LOG: true, | ||
ENABLE_SIGN_IN_LOG: true, | ||
} | ||
}, | ||
) | ||
}, | ||
) |
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,66 @@ | ||
export const emailLogs = [ | ||
{ | ||
id: 1, | ||
success: true, | ||
receiver: 'test1@email.com', | ||
response: 'response 1', | ||
content: 'content 1', | ||
createdAt: '2024-08-07 20:45:27', | ||
updatedAt: '2024-08-07 20:45:27', | ||
deletedAt: null, | ||
}, | ||
{ | ||
id: 2, | ||
success: true, | ||
receiver: 'test2@email.com', | ||
response: 'response 2', | ||
content: 'content 2', | ||
createdAt: '2024-08-07 20:45:27', | ||
updatedAt: '2024-08-07 20:45:27', | ||
deletedAt: null, | ||
}, | ||
] | ||
|
||
export const smsLogs = [ | ||
{ | ||
id: 1, | ||
success: true, | ||
receiver: '+16471231111', | ||
response: 'response 1', | ||
content: 'content 1', | ||
createdAt: '2024-08-07 20:45:27', | ||
updatedAt: '2024-08-07 20:45:27', | ||
deletedAt: null, | ||
}, | ||
{ | ||
id: 2, | ||
success: true, | ||
receiver: '+16471231112', | ||
response: 'response 2', | ||
content: 'content 2', | ||
createdAt: '2024-08-07 20:45:27', | ||
updatedAt: '2024-08-07 20:45:27', | ||
deletedAt: null, | ||
}, | ||
] | ||
|
||
export const signInLogs = [ | ||
{ | ||
id: 1, | ||
userId: 1, | ||
ip: '1-1-1-1', | ||
detail: 'test', | ||
createdAt: '2024-08-07 20:45:27', | ||
updatedAt: '2024-08-07 20:45:27', | ||
deletedAt: null, | ||
}, | ||
{ | ||
id: 2, | ||
userId: 2, | ||
ip: '1-1-1-2', | ||
detail: 'test 2', | ||
createdAt: '2024-08-07 20:45:27', | ||
updatedAt: '2024-08-07 20:45:27', | ||
deletedAt: null, | ||
}, | ||
] |
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