-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(slash): fix console warning when using Infos component when id is…
… not provided Ref: #648
- Loading branch information
Showing
4 changed files
with
39 additions
and
2 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
Empty file.
21 changes: 21 additions & 0 deletions
21
slash/react/src/utilities/helpers/__tests__/generateId.test.ts
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,21 @@ | ||
import { generateId } from "../generateId"; | ||
|
||
describe("generateKey", () => { | ||
it("should generate a unique id for each object", () => { | ||
const obj1 = {}; | ||
const obj2 = {}; | ||
const key1 = generateId(obj1); | ||
const key2 = generateId(obj2); | ||
|
||
expect(key1).toBe("id-1"); | ||
expect(key2).toBe("id-2"); | ||
}); | ||
|
||
it("should return the same id for the same object", () => { | ||
const obj = {}; | ||
const key1 = generateId(obj); | ||
const key2 = generateId(obj); | ||
|
||
expect(key1).toBe(key2); | ||
}); | ||
}); |
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,14 @@ | ||
let counter = 1; | ||
|
||
const map = new WeakMap<object, number>(); | ||
|
||
export const generateId = (item: object): string => { | ||
if (!map.has(item)) { | ||
map.set(item, counter); | ||
counter += 1; | ||
|
||
return generateId(item); | ||
} | ||
|
||
return `id-${map.get(item)}`; | ||
}; |