Simple Solid primitive unit test utility.

Overview

solid-primitive-test-util

Simple Solid primitive unit test utility.

Install

pnpm add solid-primitive-test-util -D

Example

Basic Usage

Let's say we have the following primitive to be tested:

import { createSignal } from 'solid-js';

export const createCounter = () => {
  const [count, setCount] = createSignal(0);
  const increment = () => setCount((prev) => prev + 1);
  return { count, increment };
};

You can test it by wrapping with renderPrimitive helper. The return value of renderPrimitive includes result property that is the result of the primitive function:

import { renderPrimitive } from 'solid-primitive-test-util';
import { createCounter } from './counter';

test('should increment count', () => {
  const { result } = renderPrimitive(() => createCounter());

  expect(result.count()).toBe(0);
  result.increment();
  expect(result.count()).toBe(1);
});

Wrapped Components

Sometimes, primitives may need access to values or functionality outside of itself that are provided by a context provider or some other HOC.

import { createSignal, createContext, useContext } from 'solid-js';

const CounterStepContext = createContext(1);

export const CounterStepProvider = (props) => (
  <CounterStepContext.Provider value={props.step}>{props.children}</CounterStepContext.Provider>
);

export const createCounter = (initialValue = 0) => {
  const [count, setCount] = createSignal(initialValue);
  const step = useContext(CounterStepContext);
  const increment = () => setCount((prev) => prev + step);
  return { count, increment };
};

In our test, we simply use CounterStepProvider as the wrapper when rendering the primitive:

import { renderPrimitive } from 'solid-primitive-test-util';
import { CounterStepProvider, creatCounter } from './counter';

test('should use custom step when incrementing', () => {
  const wrapper = (props) => <CounterStepProvider step={2}>{props.children}</CounterStepProvider>;
  const { result } = renderHook(() => creatCounter(), { wrapper });
  result.increment();
  expect(result.count()).toBe(2);
});

License

MIT

You might also like...

Contracts with missing implementation details and unit tests to help guide junior solidity developers.

template-challenge-staking Contracts with missing implementation details and unit tests to help guide junior solidity developers. Getting started Open

Oct 7, 2022

A robust and light-weight inventory management application designed to help businesses maintain perfect control over every unit of stock.

A robust and light-weight inventory management application designed to help businesses maintain perfect control over every unit of stock.

Inventory Buddy Access inventory anytime on web, tablet or mobile. Inventory Buddy is a robust and light-weight inventory management application desig

Nov 5, 2022

Automatically generate unit price for applicable items when shopping online.

Automatically generate unit price for applicable items when shopping online.

Unit Price Helper Unit Price Helper is an Google Chrome extension that allows to display the prices per unit for the products on multiple shopping sit

Nov 18, 2022

To-do list is a tool that helps to organize your day. It simply lists the things that you need to do and allows you to mark them as complete. It is tested by JavaScript Unit testing using Jest framework. It is build by using ES6 and Webpack!

Project Name To Do list: interactive list Description the project. In this project, I added some functionality to my application to make it interactiv

Nov 11, 2022

A testing focused Remix Stack, that integrates E2E & Unit testing with Playwright, Vitest, MSW and Testing Library. Driven by Prisma ORM. Deploys to Fly.io

A testing focused Remix Stack, that integrates E2E & Unit testing with Playwright, Vitest, MSW and Testing Library. Driven by Prisma ORM. Deploys to Fly.io

Live Demo · Twitter A testing focused Remix Stack, that integrates E2E & Unit testing with Playwright, Vitest, MSW and Testing Library. Driven by Pris

Oct 31, 2022

A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Oct 3, 2020

Simple and minimal split pane component for Solid!

solid-split-pane Split pane component for Solid! Uses Split.Js under the hood. Takes all props that split.js takes, plus a gutterClass. (Q) Why not so

Oct 28, 2022

Generate meshes from signed distance functions and constructive solid geometry operations.

Generate meshes from signed distance functions and constructive solid geometry operations.

sdf-csg Generate meshes from signed distance functions and constructive solid geometry operations. This library is heavily based upon Inigo Quilez's 3

Oct 24, 2022

Solid.js library adding a services layer for global shared state.

Solid.js library adding a services layer for global shared state.

Solid Services Services are "global" objects useful for features that require shared state or persistent connections. Example uses of services might i

Dec 30, 2022
Owner
Robert Soriano
[object Object]
Robert Soriano
🧩 TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

?? TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

CodelyTV 82 Dec 7, 2022
Squeeth is a new financial primitive in DeFi that gives traders exposure to ETH²

Squeeth Monorepo The Squeethiest ?? ?? What is Squeeth The squeeth contract is designed for users to long or short a special index: Eth², as an implem

Opyn 156 Jan 4, 2023
👇 Bread n butter utility for component-tied mouse/touch gestures in Solid.

solid-gesture solid-gesture is a port of @use-gesture/react which lets you bind richer mouse and touch events to any component or view. With the data

Robert Soriano 8 Sep 30, 2022
Jester is a test-generation tool to create integration test code.

Code Generator for Integration Tests Introduction Welcome to Jester: An easy-to-use web application that helps you create and implement integration te

OSLabs Beta 54 Dec 12, 2022
Test utility to mock `window.matchMedia` for JSDOM environments.

mock-match-media Test utility for mocking window.matchMedia in JSDOM environments. JSDOM doesn't provide support for window.matchMedia, which means te

Raymond Wang 2 Oct 12, 2022
Shopping Cart - Project of HTML, CSS, and JavaScript developed by me (Raphael Martins) at the end of the Unit 9 Module 1 of the Web Development course at Trybe

Project of HTML, CSS, and JavaScript developed by me (Raphael Martins) at the end of the Unit 9 Module 1 of the Web Development course at Trybe. I was approved with 100% of the mandatory and optional requirements met.

Raphael Martins 13 Nov 27, 2022
An extension that scraps a khan academy unit and turns it into a notion todo.

The K-N Extension What is this? This extension injects a javascript file to the Khan academy website, and it adds a "Copy" button to every Khan academ

Abdulrhman Jalal 2 Jun 2, 2022
This is the team project of construct week unit-3 (js201)

This is the team project of construct week unit-3 (js201) I. Project's Title => Clone of Mytheresa.com (E-commerce website) II. Project Description =>

Brajesh Lovanshi 5 Sep 28, 2022
Notes, challenge resolutions, and applications of the Grokking Algorithms book content with unit tests

Grokking Algorithms Statements Branches Functions Lines Notes, challenge resolutions, and applications of the Grokking Algorithms book content Index F

Lucas Souza 5 Aug 14, 2022
Nightwatch.js, Unit testing, Javascript

PayScale Shared UI Cultivated library of shareable components, utilities, and widgets designed for re-use aross products. Installing and Consuming To

Dredsoft 3 Sep 15, 2022