npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dom-free/dom-free

v1.0.1

Published

I have been an agilista and evangelist for unit testing for many years. However, along with others, I have considered JavaScript testing somewhat of a lost cause. We have seen advancements in framework unit testing, but much of the JavaScript in the wil

Downloads

5

Readme

Dom Free

A state management system for JavaScript that will allow improved testability of client side code

Why I built this system

I have been an agilista and evangelist for unit testing for many years. However, along with others, I have considered JavaScript testing somewhat of a lost cause. We have seen advancements in framework unit testing, but much of the JavaScript in the wild remains untested, and largely untestable. This is principally because business logic is intermingled with DOM access code.

I thought if we could untangle that mess, we could make legitimately testable JavaScript code that operates upon the DOM indirectly. That is why I built this system.

How it works

Browser Code:

Image

The JavaScript in your HTML file is simple, and is located in a single script tag as follows

<script lang="javascript">
	$(document).ready(() => {
		sms.Clear();
		sms.Register("firstName", $("#fname"))
            .attr('value', 'George');
		sms.Register("lastName", $("#lname"))
            .attr('value', 'Washington');

		sms.Register("moveFirstToLast", $("#moveFirstToLast"))
            .registerEvent("click", Library.MoveFirstToLast);

	})
</script>

When the use clicks the button with id = "moveFirstToLast," the library function Library.MoveFirstToLast is called by SMS. MoveFirstToLast looks like this: MoveFirstToLast: () => sms.Item('lastName').val(sms.Item('firstName').val()), You can see that we are using sms.Item(<elementName>) both to get the value (of firstName) and assign the value (to lastName).

Unit Test Code:

Image

The JavaScript in your Unit Test is very similar to the JavaScript in your HTML file (in fact, you can copy and paste). You just need to tell SMS that Mode is "Mock" as follows:

	beforeEach(function() {
		sms = SMS;
		sms.Clear();
		sms.Mode('Mock');
		lib = Library.default;
		
		firstName = sms.Register("firstName", "#fname")
            .attr('value', 'George');
		lastName = sms.Register("lastName", "#lname")
            .attr('value', 'Washington');

		sms.Register("moveFirstToLast", "#moveFirstToLast")
            .registerEvent("click", lib.MoveFirstToLast);

	})

Because sms.Mode('Mock') has been called, the second parameter to sms.Register() is ignored.

Then, in the unit test, we are able to verify the behavior of the MoveFirstToLast Library function, as follows:

it('should be able to Copy First Name to Last Name when clicked', function() {
		sms.Item('moveFirstToLast')
            .fireEvent('click');
		var target = sms.Item('lastName');
		expect(target.val())
            .toBe('George')
    });

This test passes, proving the the Library is able to modify the "DOM" as required.

Obviously, this is a test that does not prove very much, but in a realistic scenario, the result of a complicated Library function is indeed the changing of one or more DOM elements' values or attributes, which can be verified in unit tests.

How to install DOM-FREE

npm install infoAtDomFree/dom-free

Sample Code

You can see example code at https://github.com/infoAtDomFree/example