UNPKG

context-protocol

Version:

A fully-typed implementation of the context-protocol, with support for subscriptions.

1 lines 3.75 kB
import{expect,test,vi}from"vitest";var testContextSubscriptions=(createContextFactory,addContextListenerFactory,removeContextListenerFactory,setContextValueFactory,getContextValueFactory)=>{let defaultTarget=new EventTarget,createContext=createContextFactory(),addContextListener=addContextListenerFactory(),removeContextListener=removeContextListenerFactory(),setContextValue=setContextValueFactory(),getContextValue=getContextValueFactory();test("createContext creates a branded context",()=>{let context=createContext("test");expect(context).toBe("test")}),test("ContextRequestEvent constructor and properties",()=>{let receivedData,receivedUnsubscribe,context=createContext("test"),callback=vi.fn((data2,unsubscribe)=>{receivedData=data2,receivedUnsubscribe=unsubscribe}),error=new Error("error"),throwing=vi.fn(()=>{throw error}),data={key:"value"};setContextValue(defaultTarget,context,data),expect(callback).toHaveBeenCalledTimes(0),expect(throwing).toHaveBeenCalledTimes(0),expect(receivedData).toBe(void 0),addContextListener(defaultTarget,context,callback,!0),expect(callback).toHaveBeenCalledTimes(1),expect(callback).toHaveBeenCalledWith(receivedData,receivedUnsubscribe),expect(receivedData).toBe(data),setContextValue(defaultTarget,context,data),expect(callback).toHaveBeenCalledTimes(2),expect(callback).toHaveBeenCalledWith(receivedData,receivedUnsubscribe),expect(receivedData).toBe(data),addContextListener(defaultTarget,context,callback,!0),addContextListener(defaultTarget,context,callback,!0),addContextListener(defaultTarget,context,callback,!0),expect(callback).toHaveBeenCalledTimes(2),expect(callback).toHaveBeenCalledWith(receivedData,receivedUnsubscribe),expect(receivedData).toBe(data),addContextListener(defaultTarget,"another-context",callback,!0),addContextListener(defaultTarget,"another-context",callback,!0),addContextListener(defaultTarget,"another-context",callback,!0),expect(callback).toHaveBeenCalledTimes(2),expect(callback).toHaveBeenCalledWith(receivedData,receivedUnsubscribe),expect(receivedData).toBe(data),setContextValue(defaultTarget,"yet-another-context",data),setContextValue(defaultTarget,"yet-another-context",data),setContextValue(defaultTarget,"yet-another-context",data),expect(callback).toHaveBeenCalledTimes(2),expect(callback).toHaveBeenCalledWith(receivedData,receivedUnsubscribe),expect(receivedData).toBe(data),expect(receivedUnsubscribe).toBeDefined(),expect(receivedUnsubscribe()).toBeUndefined(),setContextValue(defaultTarget,context,data),addContextListener(defaultTarget,context,callback,!0),expect(callback).toHaveBeenCalledTimes(3),expect(callback).toHaveBeenCalledWith(receivedData,receivedUnsubscribe),expect(receivedData).toBe(data),addContextListener(defaultTarget,context,throwing,!0),expect(throwing).toHaveBeenCalledTimes(1),expect(throwing).toThrowError(error)}),test("context callbacks are unsubscribed when consumer is not contained in provider",()=>{let data={key:"value"},callback=vi.fn(),provider=document.createElement("div"),consumer=document.createElement("div");provider.appendChild(consumer),setContextValue(provider,"test",data),addContextListener(consumer,"test",callback,!0),expect(callback).toHaveBeenCalledTimes(1),expect(callback).toHaveBeenCalledWith(data,expect.any(Function)),removeContextListener(consumer,"test",callback),provider.removeChild(consumer),setContextValue(provider,"test",data),expect(callback).toHaveBeenCalledTimes(1),expect(callback).toHaveBeenCalledWith(data,expect.any(Function))}),test("getContextData and setContextValue work together",()=>{let target=new EventTarget,context=createContext("test");setContextValue(target,context,42);let data=getContextValue(target,context);expect(data).toBe(42)})};export{testContextSubscriptions}