Skip to content

Commit 4ebee30

Browse files
committed
🐛 Prevent doing action when incorrect option is used
Fix theme switch and redirects when incorrect options are used.
1 parent bbe94fb commit 4ebee30

File tree

5 files changed

+33
-37
lines changed

5 files changed

+33
-37
lines changed

src/components/commands/Projects.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const Projects: React.FC = () => {
2121

2222
/* ===== check current command is redirect ===== */
2323
useEffect(() => {
24-
if (checkRedirect(arg, rerender, currentCommand, "projects")) {
24+
if (checkRedirect(rerender, currentCommand, "projects")) {
2525
projects.forEach(({ id, url }) => {
2626
id === parseInt(arg[1]) && window.open(url, "_blank");
2727
});

src/components/commands/Socials.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Socials: React.FC = () => {
1818

1919
/* ===== check current command makes redirect ===== */
2020
useEffect(() => {
21-
if (checkRedirect(arg, rerender, currentCommand, "socials")) {
21+
if (checkRedirect(rerender, currentCommand, "socials")) {
2222
socials.forEach(({ id, url }) => {
2323
id === parseInt(arg[1]) && window.open(url, "_blank");
2424
});

src/components/commands/Themes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Themes: React.FC = () => {
2424

2525
/* ===== check current command makes redirect ===== */
2626
useEffect(() => {
27-
if (checkThemeSwitch(arg, rerender, currentCommand, myThemes)) {
27+
if (checkThemeSwitch(rerender, currentCommand, myThemes)) {
2828
themeSwitcher?.(theme[currentCommand[2]]);
2929
}
3030
}, [arg, rerender, currentCommand]);

src/test/Terminal.spec.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ describe("Terminal Component", () => {
196196
await user.type(terminalInput, `${cmd} ${arg} extra-arg{enter}`);
197197
expect(screen.getByTestId(`${cmd}-invalid-arg`)).toBeInTheDocument();
198198
});
199+
200+
it(`should return usage component for '${cmd}' cmd with incorrect option`, async () => {
201+
const arg = cmd === "themes" ? "go light" : "set 4";
202+
window.open = vi.fn();
203+
204+
// firstly run commands correct options
205+
await user.type(terminalInput, `projects go 4{enter}`);
206+
await user.type(terminalInput, `socials go 4{enter}`);
207+
await user.type(terminalInput, `themes set espresso{enter}`);
208+
209+
// then run cmd with incorrect options
210+
await user.type(terminalInput, `${cmd} ${arg}{enter}`);
211+
expect(window.open).toBeCalledTimes(2);
212+
213+
// TODO: Test theme change
214+
});
199215
});
200216
});
201217

src/utils/funcs.ts

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,61 +38,41 @@ export const getCurrentCmdArry = (history: string[]) =>
3838

3939
/**
4040
* Check current render makes redirect
41-
* @param {string[]} arg - arg of the command
4241
* @param {boolean} rerender - is submitted or not
4342
* @param {string[]} currentCommand - current submitted command
4443
* @param {string} command - the command of the function
4544
* @returns {boolean} redirect - true | false
4645
*/
4746
export const checkRedirect = (
48-
arg: string[],
4947
rerender: boolean,
5048
currentCommand: string[],
5149
command: string
52-
): boolean => {
53-
if (
54-
arg.length > 0 && // contains arg
55-
arg[0] === "go" && // first arg is 'go'
56-
rerender && // is submitted
57-
currentCommand[0] === command && // current command starts with ('socials'|'projects')
58-
currentCommand.length > 1 && // current command has arg
59-
currentCommand.length < 4 && // if num of arg is valid (not `projects go 1 sth`)
60-
_.includes([1, 2, 3, 4], parseInt(currentCommand[2])) // arg last part is one of id
61-
) {
62-
return true;
63-
} else {
64-
return false;
65-
}
66-
};
50+
): boolean =>
51+
rerender && // is submitted
52+
currentCommand[0] === command && // current command starts with ('socials'|'projects')
53+
currentCommand[1] === "go" && // first arg is 'go'
54+
currentCommand.length > 1 && // current command has arg
55+
currentCommand.length < 4 && // if num of arg is valid (not `projects go 1 sth`)
56+
_.includes([1, 2, 3, 4], parseInt(currentCommand[2])); // arg last part is one of id
6757

6858
/**
6959
* Check current render makes redirect for theme
70-
* @param {string[]} arg - arg of the command
7160
* @param {boolean} rerender - is submitted or not
7261
* @param {string[]} currentCommand - current submitted command
7362
* @param {string[]} themes - the command of the function
7463
* @returns {boolean} redirect - true | false
7564
*/
7665
export const checkThemeSwitch = (
77-
arg: string[],
7866
rerender: boolean,
7967
currentCommand: string[],
8068
themes: string[]
81-
): boolean => {
82-
if (
83-
arg.length > 0 && // contains arg
84-
arg[0] === "set" && // first arg is 'set'
85-
rerender && // is submitted
86-
currentCommand[0] === "themes" && // current command starts with ('themes')
87-
currentCommand.length > 1 && // current command has arg
88-
currentCommand.length < 4 && // if num of arg is valid (not `themes set light sth`)
89-
_.includes(themes, currentCommand[2]) // arg last part is one of id
90-
) {
91-
return true;
92-
} else {
93-
return false;
94-
}
95-
};
69+
): boolean =>
70+
rerender && // is submitted
71+
currentCommand[0] === "themes" && // current command starts with 'themes'
72+
currentCommand[1] === "set" && // first arg is 'set'
73+
currentCommand.length > 1 && // current command has arg
74+
currentCommand.length < 4 && // if num of arg is valid (not `themes set light sth`)
75+
_.includes(themes, currentCommand[2]); // arg last part is one of id
9676

9777
/**
9878
* Perform advanced tab actions

0 commit comments

Comments
 (0)