Skip to content

Commit

Permalink
feat: Allow add the minus operator before a number
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-santosP committed Apr 27, 2021
1 parent 073add6 commit cfbfc3d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 8 additions & 2 deletions __tests__/unit/components/Calculator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ describe("Calculator", () => {
expect(wrapper.find("[data-test='text']").text()).toEqual(expectedDigits);
});

it("Should be able to add one operators only after a digit", async () => {
it("Should be able to add operators only after a digit, expect the minus operator", async () => {
const wrapper = mount(Calculator);
const buttons = wrapper.findAll("button");
const buttonClear = buttons.find((button) => button.text() === "Clear");

for (const operator of OPERATORS) {
const buttonOperator = buttons.find((button) => button.text() === operator);
await buttonOperator?.trigger("click");

expect(wrapper.find("[data-test='text']").text()).toEqual("");
if (operator === "-") {
expect(wrapper.find("[data-test='text']").text()).toEqual("-");
await buttonClear?.trigger("click");
} else {
expect(wrapper.find("[data-test='text']").text()).toEqual("");
}
}

await addToMemory(wrapper, "5");
Expand Down
6 changes: 4 additions & 2 deletions __tests__/unit/compositions/useCalculate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ describe("useCalculate", () => {
expect(memory.value).toEqual("0.");
});

it("should prevent to add operators before number", () => {
it("should prevent to add operators before number, expect minus operator", () => {
const { addOperator, memory } = useCalculate();
addOperator("+");
addOperator("-");
addOperator("*");
addOperator("/");
expect(memory.value).toEqual("");

addOperator("-");
expect(memory.value).toEqual("-");
});

it("should prevent to add multiples operators in sequence", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/compositions/useCalculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function useCalculate() {
throw new Error("Invalid param, is not a valid operator");
}

if (!memory.value) return;
if (!memory.value && operator !== "-") return;
if (lastCharIsOperator(memory.value)) eraseLast();

clearOnNextDigit.value = false;
Expand Down

0 comments on commit cfbfc3d

Please sign in to comment.