-
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.
- Loading branch information
Showing
12 changed files
with
186 additions
and
26 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,85 @@ | ||
import { screen } from "@testing-library/dom"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
import { | ||
attachComponent, | ||
createElement, | ||
createState, | ||
createRef, | ||
createContext, | ||
} from "../src"; | ||
|
||
describe("Context", () => { | ||
let cleanup: Function | undefined; | ||
|
||
afterEach(() => { | ||
cleanup?.(); | ||
cleanup = undefined; | ||
}); | ||
|
||
test("nested components have access to the Context", () => { | ||
const exampleContext = createContext<number>(); | ||
|
||
function App() { | ||
return createElement("div", { | ||
children: [ | ||
createElement("h1", { children: "Application" }), | ||
createElement(NestedComponent), | ||
], | ||
}); | ||
} | ||
|
||
function NestedComponent() { | ||
const exampleValue = exampleContext.readContext(); | ||
|
||
return createElement("div", { | ||
"data-testid": "contextContent", | ||
children: `context value is ${exampleValue}`, | ||
}); | ||
} | ||
|
||
cleanup = attachComponent({ | ||
htmlElement: document.body, | ||
component: createElement(exampleContext.Provider, { | ||
value: 5, | ||
children: createElement(App), | ||
}), | ||
}); | ||
|
||
expect(screen.getByTestId("contextContent").textContent).toBe( | ||
"context value is 5" | ||
); | ||
}); | ||
|
||
test("nested components have access to the Context if added with Context.addContext()", () => { | ||
const exampleContext = createContext<number>(); | ||
|
||
function App() { | ||
exampleContext.addContext(5); | ||
return createElement("div", { | ||
children: [ | ||
createElement("h1", { children: "Application" }), | ||
createElement(NestedComponent), | ||
], | ||
}); | ||
} | ||
|
||
function NestedComponent() { | ||
const exampleValue = exampleContext.readContext(); | ||
|
||
return createElement("div", { | ||
"data-testid": "contextContent", | ||
children: `context value is ${exampleValue}`, | ||
}); | ||
} | ||
|
||
cleanup = attachComponent({ | ||
htmlElement: document.body, | ||
component: createElement(App), | ||
}); | ||
|
||
expect(screen.getByTestId("contextContent").textContent).toBe( | ||
"context value is 5" | ||
); | ||
}); | ||
}); |
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
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,72 @@ | ||
import { Fragment } from "../fragment"; | ||
import { createElement } from "../create-element"; | ||
|
||
import type { VelesChildren } from "../types"; | ||
import type { ComponentContext } from "./types.d.ts"; | ||
|
||
// the name is so convoluted because we also | ||
// use context stack for lifecycle | ||
const publicContextStack: ComponentContext[] = []; | ||
|
||
let contextIdCounter = 1; | ||
function createContext<T>() { | ||
// unique context id | ||
const contextId = contextIdCounter++; | ||
function addContext(value: T) { | ||
const currentContextObject = | ||
publicContextStack[publicContextStack.length - 1]; | ||
|
||
if (!currentContextObject) { | ||
// either executed outside of the rendering framework | ||
// or some bug | ||
console.error("cannot add Context due to missing stack value"); | ||
} else { | ||
publicContextStack[publicContextStack.length - 1] = { | ||
...currentContextObject, | ||
[contextId]: value, | ||
}; | ||
} | ||
} | ||
return { | ||
Provider: ({ value, children }: { value: T; children: VelesChildren }) => { | ||
addContext(value); | ||
return createElement(Fragment, { children }); | ||
}, | ||
addContext, | ||
readContext: (): T => { | ||
const currentContext = publicContextStack[publicContextStack.length - 1]; | ||
|
||
if (!currentContext) { | ||
// we are outside the context somehow | ||
console.error("no Context currently available"); | ||
} else { | ||
return currentContext[contextId]; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
function addPublicContext(specificContext?: ComponentContext) { | ||
if (specificContext) { | ||
publicContextStack.push(specificContext); | ||
} else { | ||
if (publicContextStack.length === 0) { | ||
publicContextStack.push({}); | ||
} else { | ||
const currentContext = publicContextStack[publicContextStack.length - 1]; | ||
publicContextStack.push(currentContext); | ||
} | ||
} | ||
} | ||
|
||
function popPublicContext() { | ||
publicContextStack.pop(); | ||
} | ||
|
||
// this function is needed to save current context to re-execute components | ||
// which are mounted conditionally | ||
function getCurrentContext() { | ||
return publicContextStack[publicContextStack.length - 1]; | ||
} | ||
|
||
export { createContext, addPublicContext, popPublicContext, getCurrentContext }; |
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,3 @@ | ||
export type ComponentContext = { | ||
[id: number]: any; | ||
}; |
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