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

mithril-fake-xhr

v0.1.3

Published

A fake XHR handler for testing Mithrl apps

Downloads

13

Readme

Build Status

Mithril-fake-xhr

A fake XHR handler for testing Mithril apps

Installation

npm install --save-dev mithril-fake-xhr

Use cases


  // pass the window context into the fake
  var fakeXHR = require('mithril-fake-xhr')(mock || window);
  
	// Mocking

	// expected
	test(function() {
		var response = fakeXHR('get','/test1');
		m.request({method:'GET', url:'/test1'});
		return response.count==1;
	});

	// unexpected
	test(function() {
		m.request({method:'GET', url:'/test/xxx'});
		return fakeXHR.unexpectedRequests !== 0;
	});

	// unexpected payload
	test(function() {
		fakeXHR('post','/test4', {p1:1,p2:2});
		m.request({method:'POST', url:'/test4', data:{p1:'xx',p2:'yyy'}})
		return fakeXHR.unexpectedRequests !== 0;
	});


	// unresolved
	test(function() {
		var response = fakeXHR('get','/test1/yyy');
		return response.count === 0;
	});

	// Stubbing

	// GET
	test(function() {
		var data;
		fakeXHR('get','/test2').respondWith('abc');
		m.request({method:'GET', url:'/test2'}).then(function(response){
			data = response;
		});
		return data=='abc';
	});

	// params
	test(function() {
		var data;
		fakeXHR('get','/test3\\?p1=1&p2=2').respondWith({p1:'one',p2:'two'});
		m.request({method:'GET', url:'/test3?p1=1&p2=2'}).then(function(response){
			data = response;
		});
		return data.p1==='one' && data.p2==='two';
	});

	// regex params
	test(function() {
		var data;
		fakeXHR('get','/test3\\?p1=.+&p2=\\d+').respondWith({p1:'ABC',p2:'onetwothree'});
		m.request({method:'GET', url:'/test3?p1=abc&p2=123'}).then(function(response){
			data = response;
		});
		return data.p1==='ABC' && data.p2==='onetwothree';
	});

	// POST
	test(function() {
		var data;
		fakeXHR('post','/test4').respondWith({p1:'one',p2:'two'});
		m.request({method:'POST', url:'/test4', data:{p1:1,p2:2}}).then(function(response){
			data = response;
		});
		return data.p1==='one' && data.p2==='two';
	});

	// errors
	test(function() {
		var data;
		fakeXHR('get','/test6').respondWith(404,'file not found');
		m.request({method:'GET', url:'/test6'}).then(undefined, function(response){
			data=response;
		});
		return data==='file not found';
	});

	// reset
	test(function() {
		fakeXHR('get','/test/7');
		fakeXHR.reset();
		m.request({method:'GET', url:'/test/7'});
		return fakeXHR.unexpectedRequests !== 0;
	});

	// passthrough
	test(function() {
		var data;
		var response = fakeXHR('get','/test5').passthrough();
		m.request({method:'GET', url:'/test5'}).then(function(response){
			data = response;
		});
		return data === 'ABC';
	});

	// modify response data
	test(function() {
		var data;
		var response = fakeXHR('get','/test5').passthrough(function(status,data){
			return {status:status,data:'DEF'}
		});
		m.request({method:'GET', url:'/test5'}).then(function(response){
			data = response;
		});
		return data === 'DEF';
	});

	// modify response status
	test(function() {
		var data;
		var response = fakeXHR('get','/test5').passthrough(function(status,data){
			return {status:403,data:'Forbidden'}
		});
		m.request({method:'GET', url:'/test5'}).then(null,function(response){
			data = response;
		});
		return data=='Forbidden';
	});

	// errors
	test(function() {
		var data;
		fakeXHR('get','/test6').respondWith(404,'file not found');
		m.request({method:'GET', url:'/test6'}).then(undefined, function(response){
			data=response;
		});
		return data==='file not found';
	});

	// reset
	test(function() {
		fakeXHR('get','/test/7');
		fakeXHR.reset();
		m.request({method:'GET', url:'/test/7'});
		return fakeXHR.unexpectedRequests !== 0;
	});

Copyright

Source code is licensed under the MIT License (MIT). See LICENSE.txt file in the project root. Documentation to the project is licensed under the CC BY 4.0 license.