-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): support instantiateModalStack
- Loading branch information
1 parent
c36e5d7
commit 75c4ab4
Showing
6 changed files
with
90 additions
and
0 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
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,47 @@ | ||
import type { instantiateModalStack as _instantiateModalStack } from "./ModalStack"; | ||
|
||
const defaultInitialIndex = 1000; | ||
|
||
describe("instantiateModalStack", () => { | ||
let instantiateModalStack: typeof _instantiateModalStack; | ||
|
||
beforeEach(() => { | ||
jest.isolateModules(() => { | ||
({ instantiateModalStack } = require("./ModalStack")); | ||
}); | ||
}); | ||
|
||
test("should push a new index onto the stack and return the correct value", () => { | ||
const stack = instantiateModalStack(); | ||
const result = stack.push(); | ||
expect(result).toBe(defaultInitialIndex); | ||
}); | ||
|
||
test("should increment the index correctly on multiple stack pushes", () => { | ||
const stack1 = instantiateModalStack(); | ||
const stack2 = instantiateModalStack(); | ||
const stack3 = instantiateModalStack(); | ||
|
||
const result1 = stack1.push(); | ||
const result2 = stack2.push(); | ||
expect(result1).toBe(defaultInitialIndex); | ||
expect(result2).toBe(defaultInitialIndex + 1); | ||
|
||
stack1.pull(); | ||
const result3 = stack3.push(); | ||
expect(result3).toBe(defaultInitialIndex + 2); | ||
}); | ||
|
||
test("should handle pushes without pull", () => { | ||
const stack = instantiateModalStack(); | ||
const result1 = stack.push(); | ||
const result2 = stack.push(); | ||
expect(result1).toBe(defaultInitialIndex); | ||
expect(result2).toBe(defaultInitialIndex); | ||
}); | ||
|
||
test("should respect param of initialIndex", () => { | ||
const stack = instantiateModalStack(10); | ||
expect(stack.push()).toBe(10); | ||
}); | ||
}); |
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,29 @@ | ||
const stack: number[] = []; | ||
|
||
export interface ModalStack { | ||
push: () => number; | ||
pull: () => void; | ||
} | ||
|
||
export function instantiateModalStack(initialIndex = 1000): ModalStack { | ||
let index = -1; | ||
const pull = (): void => { | ||
if (index > -1) { | ||
const found = stack.indexOf(index); | ||
// Assert: found should always be greater than -1 | ||
// istanbul ignore else | ||
if (found > -1) { | ||
stack.splice(found, 1); | ||
} | ||
} | ||
}; | ||
const push = (): number => { | ||
// Handle pushes without pull | ||
pull(); | ||
// Find the next available index | ||
index = (stack[stack.length - 1] ?? -1) + 1; | ||
stack.push(index); | ||
return index + initialIndex; | ||
}; | ||
return { push, pull }; | ||
} |
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