-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
652 additions
and
238 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
168 changes: 168 additions & 0 deletions
168
src/routes/FavoriteHubs/__tests__/FavoriteHubDialog.test.tsx
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,168 @@ | ||
import { | ||
getConnectedSocket, | ||
getMockServer, | ||
waitForExpect, | ||
} from 'airdcpp-apisocket/tests/helpers.js'; | ||
|
||
import { jest } from '@jest/globals'; | ||
import { renderRoutes } from 'tests/test-containers'; | ||
|
||
import * as UI from 'types/ui'; | ||
|
||
import { | ||
createTestModalController, | ||
TestModalNavigateButton, | ||
} from 'tests/test-component-helpers'; | ||
import FavoriteHubDialog from '../components/FavoriteHubDialog'; | ||
import { getModuleT } from 'utils/TranslationUtils'; | ||
import { useTranslation } from 'react-i18next'; | ||
import ShareProfileConstants from 'constants/ShareProfileConstants'; | ||
import { ShareProfilesListResponse } from 'tests/mocks/api/share-profiles'; | ||
import FavoriteHubConstants from 'constants/FavoriteHubConstants'; | ||
import { | ||
MOCK_FAVORITE_HUB_ID, | ||
FavoriteHubGetResponse, | ||
} from 'tests/mocks/api/favorite-hubs'; | ||
import { setInputFieldValues, setupUserEvent } from 'tests/test-form-helpers'; | ||
|
||
// tslint:disable:no-empty | ||
describe('FavoriteHubDialog', () => { | ||
let server: ReturnType<typeof getMockServer>; | ||
const getSocket = async () => { | ||
const { socket } = await getConnectedSocket(server); | ||
|
||
// Data fetch | ||
server.addDataHandler( | ||
'GET', | ||
ShareProfileConstants.PROFILES_URL, | ||
ShareProfilesListResponse, | ||
); | ||
server.addDataHandler( | ||
'GET', | ||
`${FavoriteHubConstants.HUBS_URL}/${MOCK_FAVORITE_HUB_ID}`, | ||
FavoriteHubGetResponse, | ||
); | ||
|
||
// Listeners | ||
server.addDataHandler( | ||
'POST', | ||
`${ShareProfileConstants.PROFILES_URL}/listeners/share_profile_added`, | ||
undefined, | ||
); | ||
server.addDataHandler( | ||
'POST', | ||
`${ShareProfileConstants.PROFILES_URL}/listeners/share_profile_updated`, | ||
undefined, | ||
); | ||
server.addDataHandler( | ||
'POST', | ||
`${ShareProfileConstants.PROFILES_URL}/listeners/share_profile_removed`, | ||
undefined, | ||
); | ||
|
||
const onCreated = jest.fn(); | ||
const onUpdated = jest.fn(); | ||
|
||
// Save handlers | ||
server.addDataHandler( | ||
'PATCH', | ||
`${FavoriteHubConstants.HUBS_URL}/${MOCK_FAVORITE_HUB_ID}`, | ||
undefined, | ||
onUpdated, | ||
); | ||
server.addDataHandler('POST', FavoriteHubConstants.HUBS_URL, undefined, onCreated); | ||
|
||
return { socket, server, onCreated, onUpdated }; | ||
}; | ||
|
||
const renderDialog = async (id: number | null) => { | ||
const { socket, server, ...other } = await getSocket(); | ||
|
||
const FavoriteHubDialogTest = () => { | ||
const { t } = useTranslation(); | ||
const favT = getModuleT(t, UI.Modules.FAVORITE_HUBS); | ||
return ( | ||
<> | ||
<TestModalNavigateButton | ||
modalRoute={id ? `/home/entries/${id}` : '/home/entries'} | ||
/> | ||
<FavoriteHubDialog favT={favT} /> | ||
</> | ||
); | ||
}; | ||
|
||
const routes = [ | ||
{ | ||
path: '/home/*', | ||
Component: FavoriteHubDialogTest, | ||
}, | ||
]; | ||
|
||
const renderData = await renderRoutes(routes, { | ||
socket, | ||
routerProps: { initialEntries: ['/home'] }, | ||
}); | ||
|
||
const modalController = createTestModalController(renderData); | ||
return { modalController, ...renderData, ...other }; | ||
}; | ||
|
||
beforeEach(() => { | ||
server = getMockServer(); | ||
}); | ||
|
||
afterEach(() => { | ||
server.stop(); | ||
}); | ||
|
||
test('should update existing', async () => { | ||
const userEvent = setupUserEvent(); | ||
const { getByText, getByLabelText, modalController, onUpdated } = | ||
await renderDialog(MOCK_FAVORITE_HUB_ID); | ||
|
||
await modalController.openDialog(); | ||
|
||
// Check content | ||
await waitForExpect(() => expect(getByText('Edit favorite hub')).toBeTruthy()); | ||
|
||
// Edit | ||
await setInputFieldValues( | ||
{ userEvent, getByLabelText }, | ||
{ | ||
Name: 'Updated name', | ||
}, | ||
); | ||
|
||
await modalController.closeDialogButton('Save'); | ||
|
||
expect(onUpdated).toHaveBeenCalledTimes(1); | ||
expect(onUpdated.mock.calls[0]).toMatchSnapshot(); | ||
}, 100000); | ||
|
||
test('should create new', async () => { | ||
const userEvent = setupUserEvent(); | ||
const { getByText, getByLabelText, modalController, onCreated, onUpdated } = | ||
await renderDialog(null); | ||
|
||
await modalController.openDialog(); | ||
|
||
// Check content | ||
await waitForExpect(() => expect(getByText('Add favorite hub')).toBeTruthy()); | ||
|
||
// Edit | ||
await setInputFieldValues( | ||
{ userEvent, getByLabelText }, | ||
{ | ||
Name: 'Hub name', | ||
'Hub URL': 'adcs://example.com:1511', | ||
}, | ||
); | ||
|
||
await modalController.closeDialogButton('Save'); | ||
|
||
expect(onCreated).toHaveBeenCalledTimes(1); | ||
expect(onCreated.mock.calls[0]).toMatchSnapshot(); | ||
|
||
// stop(); | ||
}, 100000); | ||
}); |
40 changes: 40 additions & 0 deletions
40
src/routes/FavoriteHubs/__tests__/__snapshots__/FavoriteHubDialog.test.tsx.snap
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,40 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`FavoriteHubDialog should create new 1`] = ` | ||
[ | ||
{ | ||
"callback_id": 6, | ||
"data": { | ||
"auto_connect": false, | ||
"away_message": null, | ||
"connection_ip_v4": null, | ||
"connection_ip_v6": null, | ||
"connection_mode_v4": null, | ||
"connection_mode_v6": null, | ||
"hub_description": null, | ||
"hub_url": "adcs://example.com:1511", | ||
"name": "Hub name", | ||
"nick": null, | ||
"share_profile": null, | ||
"show_joins": null, | ||
"use_main_chat_notify": null, | ||
"user_description": null, | ||
}, | ||
"method": "POST", | ||
"path": "favorite_hubs", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`FavoriteHubDialog should update existing 1`] = ` | ||
[ | ||
{ | ||
"callback_id": 7, | ||
"data": { | ||
"name": "Updated name", | ||
}, | ||
"method": "PATCH", | ||
"path": "favorite_hubs/703302715", | ||
}, | ||
] | ||
`; |
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,31 @@ | ||
export const MOCK_FAVORITE_HUB_ID = 703302715; | ||
|
||
export const FavoriteHubGetResponse = { | ||
auto_connect: false, | ||
away_message: '', | ||
connect_state: { | ||
current_hub_id: 0, | ||
id: 0, | ||
str: 'Disconnected', | ||
}, | ||
connection_ip_v4: '', | ||
connection_ip_v6: '', | ||
connection_mode_v4: null, | ||
connection_mode_v6: null, | ||
fav_show_joins: null, | ||
has_password: false, | ||
hub_description: 'ADC Test hub', | ||
hub_url: 'adc://[::1]:2782', | ||
id: MOCK_FAVORITE_HUB_ID, | ||
log_main: null, | ||
name: 'ADCH++ Debug', | ||
nick: '', | ||
nmdc_encoding: 'utf-8', | ||
share_profile: { | ||
id: null, | ||
str: '', | ||
}, | ||
show_joins: null, | ||
use_main_chat_notify: null, | ||
user_description: '', | ||
}; |
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,34 @@ | ||
export const ShareProfilesListResponse = [ | ||
{ | ||
default: true, | ||
files: 2004, | ||
id: 28550432, | ||
name: 'Default profile', | ||
size: 60682878029, | ||
str: 'Default profile (Default)', | ||
}, | ||
{ | ||
default: false, | ||
files: 1848, | ||
id: 0, | ||
name: 'Profile 2', | ||
size: 51592666838, | ||
str: 'Profile 2', | ||
}, | ||
{ | ||
default: false, | ||
files: 0, | ||
id: 265571564, | ||
name: 'Empty', | ||
size: 0, | ||
str: 'Empty', | ||
}, | ||
{ | ||
default: false, | ||
files: 0, | ||
id: 1, | ||
name: 'Share hidden', | ||
size: 0, | ||
str: 'Share hidden', | ||
}, | ||
]; |
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
Oops, something went wrong.