
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
morphonent-test
Advanced tools
morphonent-test is a testing library for verifying components written with morphonent.
morphonent-test is a simple npm package that you can install with yarn:
$> yarn add -D morphonent-test
or npm:
$> npm install --save-dev morphonent-test
morphonent-test exposes a component wrapper entirely made for introspecting and interacting with real components with a fluent API. We've also designed the library to be TDD friendly, so it's easy to change and mold to your needs.
Let's see an example test, using jest as the test runner.
import { testing, click } from 'morphonent-test';
describe('Counter component', () => {
describe('counting upwards', () => {
it('should count +1 when the button is clicked', async () => {
const counter = (count) => element('button', { id: 'add-1', onclick: () => counter(count + 1) }, count);
const driver = testing(counter(0));
const currentCount = await driver.findById('add-1').trigger(click()).textContent();
expect(currentCount).toBe('1');
});
})
})
You can also mock more concrete behaviour if you extract your event handlers.
import { testing, click } from 'morphonent-test';
describe('Counter component', () => {
describe('counting upwards', () => {
it('should count +1 when the button is clicked', async () => {
const counter = (count, next) => element('button', { id: 'add-1', onclick: () => next(count + 1, next) }, count);
const fakeCounter = jest.fn(count => count)
const driver = testing(counter(0, fakeCounter));
const currentCount = await driver.findById('add-1').trigger(click()).textContent();
expect(fakeCounter.mock.calls[0][0]).toBe(1);
expect(currentCount).toBe(1);
});
})
})
Builds a test component based on a root component. This is the main entrypoint to the testing library.
Allows you to find a component given a condition. This is a low-level function and should be used only when strictly needed. Finding a component based on arbitrary conditions can be a hot-spot of bugs and expensive tests.
const blueGreen = testing(() => element('div', {}, element('div', { class: 'blue' }, 'Blue'), element('div', { class: 'green' }, 'Green')))
const green = blueGreen.findWhere(el => el.props && el.props.get('class') === 'green')
expect(await green.textContent()).toBe('Green')
Allows you to find a component given it's ID.
const blueGreen = testing(() => element('div', {}, element('div', { id: 'blue' }, 'Blue'), element('div', { id: 'green' }, 'Green')))
const green = blueGreen.findById('green')
expect(await green.textContent()).toBe('Green')
Allows you to find a component given it's class name.
const blueGreen = testing(() => element('div', {}, element('div', { class: 'blue' }, 'Blue'), element('div', { class: 'green' }, 'Green')))
const green = blueGreen.findByClass('green')
expect(await green.textContent()).toBe('Green')
Allows you to find the children elements of a component, if any.
const blueGreen = testing(() => element('div', {}, 'Blue', 'Green'))
const all = await blueGreen.findChildren().textContent()
expect(all).toStrictEqual(['Blue', 'Green'])
Allows you to find the first element of a children tree.
const blueGreen = testing(() => element('div', {}, 'Blue', 'Green'))
const blue = await blueGreen.findChildren().findFirst().textContent()
expect(blue).toBe('Blue')
Allows you to find the Nth element of a children tree, starting from 0.
const blueGreen = testing(() => element('div', {}, 'Blue', 'Green'))
const green = await blueGreen.findChildren().findNth(1).textContent()
expect(green).toBe('Green')
Allows you to navigate through the first step of a transition.
const firstStep = () => 'Hello'
const secondStep = async () => 'Bye'
const all = testing(() => transition(firstStep, secondStep))
const result = await all.unfinishedTransition().textContent()
expect(result).toBe('Hello')
Allows you to navigate through the second step of a transition.
const firstStep = () => 'Hello'
const secondStep = async () => 'Bye'
const all = testing(() => transition(firstStep, secondStep))
const result = await all.finishedTransition().textContent()
expect(result).toBe('Bye')
Simulates a set of events for writing text using the keyboard on the current selected component. By default, it triggers the onkeypress
event, but you can define any event that might suit your test.
const text = faker.internet.userName()
const input = (value) => element('input', { type: 'text', onkeypress: (ev) => input(ev.currentTarget.value.toUpperCase()), value })
const result = await testing(input('')).write(text).textContent()
expect(result).toBe(text.toUpperCase())
Changing the type of event to be triggered:
import { keyup } from 'morphonent-test'
const text = faker.internet.userName()
const input = (value) => element('input', { type: 'text', onkeyup: (ev) => input(ev.currentTarget.value.toUpperCase()), value })
const result = await testing(input('')).write(text, keyup).textContent()
expect(result).toBe(text.toUpperCase())
Simulates an event on the current selected component and switches the pointer to the root component that returns the event handler. Right now, there are four default event types:
But you can trigger other events using the event(name, props) function. For example, the keypress event is implemented as:
export function keypress (keyCode) {
return event('onkeypress', { keyCode })
}
An example test that clicks on a button that shows a message would be:
const hello = (clicked) => element('div', {},
element('button', { id: 'button', onclick: () => hello(true) }, 'Click me!'),
element('span', { id: 'log' }, clicked ? 'Yes!' : 'No!')
)
const test = testing(hello(false))
const clickedTest = test.findById('button').trigger(click())
const content = await clickedTest.findById('log').textContent()
expect(content).toBe('Yes!')
Returns a promise with the value of the specified attribute on an element.
const myComponent = element('div', { awesome: 'yes' })
const awesome = await testing(myComponent).attribute('awesome')
expect(awesome).toBe('yes')
Returns a promise with the text content of an element. If the component has a value
attribute, it will return it's value instead (for inputs, for example).
const myComponent = element('div', { awesome: 'yes' }, 'Hello World!')
const awesome = await testing(myComponent).textContent()
expect(awesome).toBe('Hello World!')
Returns the number of elements matched.
const list = () => element('p', {}, 1, 2, 3)
const result = await testing(list).findChildren().count()
expect(result).toBe(3)
FAQs
testkit for morphonent applications
The npm package morphonent-test receives a total of 0 weekly downloads. As such, morphonent-test popularity was classified as not popular.
We found that morphonent-test demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.