Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/mesh-common/src/data/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ export class MeshValue {
* @returns boolean
*/
eq = (other: MeshValue): boolean => {
return Object.keys(this.value).every((key) => this.eqUnit(key, other));
const units = Object.keys(this.value);
return (
units.length === Object.keys(other.value).length &&
units.every((key) => this.eqUnit(key, other))
);
};

/**
Expand Down
31 changes: 31 additions & 0 deletions packages/mesh-common/test/data/value/comparator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,37 @@ describe("MeshValue class", () => {
expect(value.leqUnit(mockUnit, target)).toBe(false);
});
});
describe("eq", () => {
it("should return true if both values hold the same units and quantities", () => {
const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.eq(target)).toBe(true);
});

it("should return false if a quantity differs", () => {
const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 5n });
expect(value.eq(target)).toBe(false);
});

it("should return false if there is missing unit in the target value", () => {
const value = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
const target = new MeshValue({ lovelace: 20n });
expect(value.eq(target)).toBe(false);
});

it("should return false if there is missing unit in the value.value", () => {
const value = new MeshValue({ lovelace: 20n });
const target = new MeshValue({ lovelace: 20n, [mockUnit]: 10n });
expect(value.eq(target)).toBe(false);
});

it("should return false if an empty value is compared with a non empty one", () => {
const value = new MeshValue();
const target = new MeshValue({ lovelace: 20n });
expect(value.eq(target)).toBe(false);
});
});
describe("isEmpty", () => {
it("should return true if the value is empty", () => {
const value = new MeshValue();
Expand Down
Loading