Skip to content

Commit

Permalink
Update BuyACoffee.test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoitie authored Jan 29, 2025
1 parent 82f9a8e commit e2f5561
Showing 1 changed file with 5 additions and 65 deletions.
70 changes: 5 additions & 65 deletions src/components/__tests__/BuyACoffee.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,16 @@ import { useTranslation } from 'react-i18next';

import BuyACoffee from '../BuyACoffee';

jest.mock('react-i18next');
jest.mock('axios');

describe('components/BuyACoffee', () => {
test('opens link with error', async () => {
let component;

act(() => {
component = create(<BuyACoffee />);
});

const setLanguage = (language) => {
useTranslation.mockImplementationOnce(() => ({
t: (value) => value,
i18n: { language },
}));

act(() => {
component.update(<BuyACoffee />);
});
};

const clickButtonError = async (language) => {
axios.get.mockRejectedValueOnce(new Error('Not found'));

await act(async () => {
await component.root.findByType('button').props.onClick();
});

expect(axios.get).toHaveBeenCalled();
};

setLanguage('en');
await clickButtonError();
expect(window.location.assign).toHaveBeenCalledWith('https://www.buymeacoffee.com/tmoitie');
test('opens link', async () => {
const component = create(<BuyACoffee />);

setLanguage('en-GB');
await clickButtonError();
expect(window.location.assign).toHaveBeenCalledWith('https://www.buymeacoffee.com/iwpgbp');

setLanguage('fr');
await clickButtonError();
expect(window.location.assign).toHaveBeenCalledWith('https://www.buymeacoffee.com/iwpeur');
});

test('opens link with geolocation', async () => {
let component;
expect(component.toJSON()).toMatchSnapshot();

act(() => {
component = create(<BuyACoffee />);
await act(async () => {
await component.root.findByType('button').props.onClick();
});

const clickButtonWithCountry = async (countryCode) => {
axios.get.mockResolvedValue({ data: { country_code: countryCode }});

await act(async () => {
await component.root.findByType('button').props.onClick();
});

expect(axios.get).toHaveBeenCalled();
};

expect(component.toJSON()).toMatchSnapshot();
await clickButtonWithCountry('MX');
expect(window.location.assign).toHaveBeenCalledWith('https://www.buymeacoffee.com/tmoitie');

await clickButtonWithCountry('GB');
expect(window.location.assign).toHaveBeenCalledWith('https://www.buymeacoffee.com/iwpgbp');

await clickButtonWithCountry('FR');
expect(window.location.assign).toHaveBeenCalledWith('https://www.buymeacoffee.com/iwpeur');
});
});

0 comments on commit e2f5561

Please sign in to comment.