Changeset View
Changeset View
Standalone View
Standalone View
cashtab/src/components/Alias/__tests__/Alias.test.js
Show All 38 Lines | window.matchMedia = query => ({ | ||||
removeListener: jest.fn(), // deprecated | removeListener: jest.fn(), // deprecated | ||||
addEventListener: jest.fn(), | addEventListener: jest.fn(), | ||||
removeEventListener: jest.fn(), | removeEventListener: jest.fn(), | ||||
dispatchEvent: jest.fn(), | dispatchEvent: jest.fn(), | ||||
}); | }); | ||||
// Activate alias features | // Activate alias features | ||||
aliasSettings.aliasEnabled = true; | aliasSettings.aliasEnabled = true; | ||||
/* eslint testing-library/no-container: 0 */ | |||||
describe('<Alias />', () => { | describe('<Alias />', () => { | ||||
afterEach(async () => { | afterEach(async () => { | ||||
jest.clearAllMocks(); | jest.clearAllMocks(); | ||||
await localforage.clear(); | await localforage.clear(); | ||||
}); | }); | ||||
it('Registered and Pending Aliases are correctly rendered', async () => { | it('Registered and Pending Aliases are correctly rendered', async () => { | ||||
const mockedChronik = await initializeCashtabStateForTests( | const mockedChronik = await initializeCashtabStateForTests( | ||||
walletWithXecAndTokens, | walletWithXecAndTokens, | ||||
▲ Show 20 Lines • Show All 122 Lines • ▼ Show 20 Lines | it('Registered and Pending Aliases are correctly rendered when registered list is empty', async () => { | ||||
).toHaveTextContent('No registered aliases'); | ).toHaveTextContent('No registered aliases'); | ||||
}); | }); | ||||
await waitFor(() => { | await waitFor(() => { | ||||
expect( | expect( | ||||
screen.getByTestId('pending-aliases-list'), | screen.getByTestId('pending-aliases-list'), | ||||
).toHaveTextContent('chicken444.xec'); | ).toHaveTextContent('chicken444.xec'); | ||||
}); | }); | ||||
}); | }); | ||||
it('Registered and Pending lists still renders when aliasValidationError is populated and aliasServerError is false', async () => { | it('Registered and Pending lists still renders upon alias pricing response error and aliasServerError is false', async () => { | ||||
const mockedChronik = await initializeCashtabStateForTests( | const mockedChronik = await initializeCashtabStateForTests( | ||||
walletWithXecAndTokens, | walletWithXecAndTokens, | ||||
localforage, | localforage, | ||||
); | ); | ||||
// Note: Not mocking the '/prices' API call here in order to populate aliasValidationError | // Note: Not mocking the '/prices' API call here in order to populate aliasPriceError | ||||
// Mock the refreshAliases() call to alias-server's '/address' endpoint upon component load | // Mock the refreshAliases() call to alias-server's '/address' endpoint upon component load | ||||
const addressFetchUrl = `${aliasSettings.aliasServerBaseUrl}/address/${walletWithXecAndTokens.Path1899.cashAddress}`; | const addressFetchUrl = `${aliasSettings.aliasServerBaseUrl}/address/${walletWithXecAndTokens.Path1899.cashAddress}`; | ||||
global.fetch = jest.fn(); | global.fetch = jest.fn(); | ||||
when(fetch) | when(fetch) | ||||
.calledWith(addressFetchUrl) | .calledWith(addressFetchUrl) | ||||
.mockResolvedValue({ | .mockResolvedValue({ | ||||
json: () => | json: () => | ||||
Promise.resolve(aliasAddressTwoRegisteredOnePending), | Promise.resolve(aliasAddressTwoRegisteredOnePending), | ||||
}); | }); | ||||
render(<CashtabTestWrapper chronik={mockedChronik} route="/alias" />); | const { container } = render( | ||||
<CashtabTestWrapper chronik={mockedChronik} route="/alias" />, | |||||
); | |||||
// Ensure the alias pricing error exists and the Register Alias button is disabled | |||||
// Note: the pricing error in <AliasInput> is wrapped within an AntdFormWrapper, hence | |||||
// getByTestId is not possible. getByText is also problematic in that it returns multiple | |||||
// instances and attempts to reference the [0] index results in not found response. | |||||
// As a final mitigation, using the querySelector here because the output from findByText | |||||
// shows the pricing error message residing within the [class="ant-form-item-explain-error"] tag. | |||||
expect( | |||||
container.querySelector('[class="ant-form-item-explain-error"]'), | |||||
).toBeInTheDocument(); | |||||
expect( | |||||
screen.getByRole('button', { name: /Register Alias/ }), | |||||
).toHaveProperty('disabled', true); | |||||
// Registered and Pending Alias dropdowns are rendered | // Registered and Pending Alias dropdowns are rendered | ||||
expect( | expect( | ||||
screen.getByTestId('registered-aliases-list'), | screen.getByTestId('registered-aliases-list'), | ||||
).toBeInTheDocument(); | ).toBeInTheDocument(); | ||||
expect(screen.getByTestId('pending-aliases-list')).toBeInTheDocument(); | expect(screen.getByTestId('pending-aliases-list')).toBeInTheDocument(); | ||||
// Validate the aliases within the dropdowns | // Validate the aliases within the dropdowns | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |