-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from diogofcunha/get-field
feat: 🎸 added getFieldById method
- Loading branch information
Showing
4 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { createFormulaStore } from ".."; | ||
|
||
describe("getFieldById", () => { | ||
test("should fail if field doesn`t exist", () => { | ||
const store = createFormulaStore({ onChange: jest.fn() }); | ||
|
||
expect(() => store.getFieldById("abc")).toThrowErrorMatchingInlineSnapshot( | ||
`"Field "abc" not found."` | ||
); | ||
}); | ||
|
||
test("should get added and edited fields", () => { | ||
const store = createFormulaStore({ onChange: jest.fn() }); | ||
|
||
// Add fields for dependencies. | ||
store.addField({ | ||
dependencies: [], | ||
id: "a", | ||
value: 1 | ||
}); | ||
|
||
store.addField({ | ||
dependencies: [], | ||
id: "b", | ||
value: 3 | ||
}); | ||
|
||
store.addField({ | ||
dependencies: ["a", "b"], | ||
id: "c", | ||
value: 0, | ||
calculate: (a, b) => a + b | ||
}); | ||
|
||
expect(store.getFieldById("a")).toEqual({ | ||
id: "a", | ||
incomingNeighbors: [], | ||
value: 1 | ||
}); | ||
|
||
expect(store.getFieldById("b")).toEqual({ | ||
id: "b", | ||
incomingNeighbors: [], | ||
value: 3 | ||
}); | ||
|
||
expect(store.getFieldById("c")).toEqual({ | ||
id: "c", | ||
calculate: expect.any(Function), | ||
incomingNeighbors: ["a", "b"], | ||
value: 4 | ||
}); | ||
|
||
store.editField({ | ||
dependencies: ["a", "b"], | ||
id: "c", | ||
value: 0, | ||
calculate: (a, b) => (a + b) * 2 | ||
}); | ||
|
||
expect(store.getFieldById("c")).toEqual({ | ||
id: "c", | ||
calculate: expect.any(Function), | ||
incomingNeighbors: ["b", "a"], | ||
value: 8 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters