Skip to content
This repository has been archived by the owner on Mar 23, 2022. It is now read-only.

Commit

Permalink
FIX - Wrong preset link copied after re-copy (#191)
Browse files Browse the repository at this point in the history
Signed-off-by: RaenonX <[email protected]>
  • Loading branch information
RaenonX committed Jul 7, 2021
1 parent 58c5579 commit 1f83be1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
37 changes: 36 additions & 1 deletion src/components/elements/gameData/skillAtk/main.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,40 @@ describe('ATK skill lookup', () => {
expect(fnMakePreset.mock.calls[0][1].display.actualDamage).toBe(true);
});

it.todo('reset preset status on input changed then re-search');
it('reset preset status on input changed then re-search', async () => {
jest.spyOn(ApiRequestSender, 'setPresetAtkSkill').mockResolvedValue({
code: ApiResponseCode.SUCCESS,
success: true,
presetId: 'abc',
});

renderReact(
() => <AttackingSkillLookup/>,
{hasSession: true},
);

const displayActualDamageBtn = await screen.findByText(translationEN.game.skillAtk.display.options.actualDamage);
userEvent.click(displayActualDamageBtn);
await waitFor(() => expect(displayActualDamageBtn.parentNode).toHaveClass('active'));

const searchButton = await screen.findByText(
translationEN.misc.search,
{selector: 'button:enabled'},
{timeout: 2000},
);
userEvent.click(searchButton);

await waitForEntryProcessed();

const shareButton = screen.getByText('', {selector: 'i.bi-share-fill'});
userEvent.click(shareButton);
expect(shareButton).not.toBeInTheDocument();

userEvent.click(displayActualDamageBtn);
await waitFor(() => expect(displayActualDamageBtn.parentNode).not.toHaveClass('active'));

userEvent.click(searchButton);

expect(screen.getByText('', {selector: 'i.bi-share-fill'})).toBeInTheDocument();
});
});
50 changes: 45 additions & 5 deletions src/components/elements/gameData/skillAtk/preset/main.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe('ATK skill input preset manager', () => {

beforeEach(() => {
window.location.href = 'http://localhost:3000';
// @ts-ignore
// noinspection JSConstantReassignment
navigator.clipboard = {writeText: jest.fn().mockResolvedValue(void 0)};
fnMakePreset = jest.spyOn(ApiRequestSender, 'setPresetAtkSkill').mockResolvedValue({
code: ApiResponseCode.SUCCESS,
success: true,
Expand All @@ -37,18 +40,55 @@ describe('ATK skill input preset manager', () => {
expect(fnMakePreset.mock.calls[0][1]).toBe(inputData);
});

it('shows clipboard icon and link after making a preset and copied once', async () => {
jest.useFakeTimers();

renderReact(
() => <AttackingSkillPreset inputData={generateInputData()} isEnabled/>,
{hasSession: true},
);

const shareButton = screen.getByText('', {selector: 'i.bi-share-fill'});
userEvent.click(shareButton);

await waitFor(() => expect(fnMakePreset).toHaveBeenCalled());
jest.runTimersToTime(7000);
await waitFor(() => expect(screen.getByText('', {selector: 'i.bi-clipboard'})).toBeInTheDocument());
expect(screen.getByDisplayValue(`http://localhost/?${PRESET_QUERY_NAME}=presetLink`)).toBeInTheDocument();

jest.useRealTimers();
});

it('copies the correct link if re-copy', async () => {
jest.useFakeTimers();

renderReact(
() => <AttackingSkillPreset inputData={generateInputData()} isEnabled/>,
{hasSession: true},
);

const shareButton = screen.getByText('', {selector: 'i.bi-share-fill'});
userEvent.click(shareButton);

await waitFor(() => expect(fnMakePreset).toHaveBeenCalled());
jest.runTimersToTime(7000);

const clipboardButton = screen.getByText('', {selector: 'i.bi-clipboard'});
userEvent.click(clipboardButton);

const expectedLink = `http://localhost/?${PRESET_QUERY_NAME}=presetLink`;
await waitFor(() => expect(navigator.clipboard.writeText).toHaveBeenLastCalledWith(expectedLink));

jest.useRealTimers();
});

it('does not have 2 preset IDs in the new link if created twice', async () => {
jest.spyOn(ApiRequestSender, 'getPresetAtkSkill').mockResolvedValue({
code: ApiResponseCode.SUCCESS,
success: true,
preset: {a: true},
});
window.location.href = `http://localhost/?${PRESET_QUERY_NAME}=preset`;
// @ts-ignore
// noinspection JSConstantReassignment
navigator.clipboard = {
writeText: jest.fn(),
};

renderReact(
() => <AttackingSkillPreset inputData={generateInputData()} isEnabled/>,
Expand Down
6 changes: 5 additions & 1 deletion src/components/elements/gameData/skillAtk/preset/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const AttackingSkillPreset = ({inputData, isEnabled}: Props) => {
},
});

React.useEffect(() => {
setState({...state, status: 'notCreated', link: t((t) => t.game.skillAtk.info.preset)});
}, [inputData]);

const makePreset = () => {
setState({...state, status: 'creating'});
if (!context?.session) {
Expand Down Expand Up @@ -78,7 +82,7 @@ export const AttackingSkillPreset = ({inputData, isEnabled}: Props) => {

const copyAndSetTimeout = (link: string = state.link) => {
navigator.clipboard.writeText(link).then(() => setState({...state, status: 'copied', link}));
return setTimeout(() => setState({...state, status: 'createdNotCopied'}), 5000);
return setTimeout(() => setState({...state, status: 'createdNotCopied', link}), 5000);
};

const onClickShareButton = () => {
Expand Down

0 comments on commit 1f83be1

Please sign in to comment.