Testing query-params.js with tape

pull/1/head
Dave Hoover 9 years ago
parent 63ac2864d4
commit 36d0e59c4b
  1. 2
      .travis.yml
  2. 3
      package.json
  3. 3
      src/app.js
  4. 3
      src/app/compiler.js
  5. 5
      src/app/gist-handler.js
  6. 29
      src/app/query-params.js
  7. 1
      test/index.js
  8. 21
      test/query-params-test.js

@ -1,7 +1,7 @@
language: node_js
node_js:
- stable
script: npm run lint && npm run build
script: npm run lint && npm run test && npm run build
addons:
sauce_connect:
username: "chriseth"

@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Minimalistic browser-based Solidity IDE",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "node test/index.js",
"build": "mkdir -p build; browserify src/index.js -o build/app.js",
"lint": "semistandard"
},
@ -19,6 +19,7 @@
"jquery": "^2.2.0",
"js-base64": "^2.1.9",
"semistandard": "^7.0.0",
"tape": "^4.5.1",
"web3": "^0.15.3",
"webworkify": "^1.2.1"
},

@ -3,7 +3,8 @@
var $ = require('jquery');
var utils = require('./app/utils');
var queryParams = require('./app/query-params');
var QueryParams = require('./app/query-params');
var queryParams = new QueryParams();
var gistHandler = require('./app/gist-handler');
var StorageHandler = require('./app/storage-handler');

@ -1,5 +1,5 @@
var webworkify = require('webworkify');
var queryParams = require('./query-params');
var QueryParams = require('./query-params');
var utils = require('./utils');
var Renderer = require('./renderer');
@ -7,6 +7,7 @@ var Base64 = require('js-base64').Base64;
function Compiler (editor, handleGithubCall, outputField, hidingRHP, updateFiles) {
var renderer = new Renderer(editor, this, updateFiles);
var queryParams = new QueryParams();
var compileJSON;
var compilerAcceptsMultipleFiles;

@ -1,11 +1,12 @@
/* global prompt */
var queryParams = require('./query-params');
var QueryParams = require('./query-params');
var queryParams = new QueryParams();
function handleLoad (cb) {
var params = queryParams.get();
var loadingFromGist = false;
if (typeof params['gist'] !== undefined) {
if (typeof params['gist'] !== undefined) { // testme
var gistId;
if (params['gist'] === '') {
var str = prompt('Enter the URL or ID of the Gist you would like to load.');

@ -1,10 +1,14 @@
function getQueryParams () {
var qs = window.location.hash.substr(1);
// Allowing window to be overriden for testing
function QueryParams (_window) {
if (_window === undefined) _window = window;
if (window.location.search.length > 0) {
this.get = function () {
var qs = _window.location.hash.substr(1);
if (_window.location.search.length > 0) {
// use legacy query params instead of hash
window.location.hash = window.location.search.substr(1);
window.location.search = '';
_window.location.hash = _window.location.search.substr(1);
_window.location.search = '';
}
var params = {};
@ -16,10 +20,10 @@ function getQueryParams () {
}
}
return params;
}
}
function updateQueryParams (params) {
var currentParams = getQueryParams();
this.update = function (params) {
var currentParams = this.get();
var keys = Object.keys(params);
for (var x in keys) {
currentParams[keys[x]] = params[keys[x]];
@ -29,10 +33,9 @@ function updateQueryParams (params) {
for (var y in updatedKeys) {
queryString += updatedKeys[y] + '=' + currentParams[updatedKeys[y]] + '&';
}
window.location.hash = queryString.slice(0, -1);
_window.location.hash = queryString.slice(0, -1);
}
}
module.exports = {
get: getQueryParams,
update: updateQueryParams
};
module.exports = QueryParams;

@ -0,0 +1 @@
require('./query-params-test');

@ -0,0 +1,21 @@
var test = require('tape');
var QueryParams = require('../src/app/query-params');
test('queryParams.get', function (t) {
t.plan(2);
var fakeWindow = {location: {hash: '#wat=sup&foo=bar', search: ''}};
var params = new QueryParams(fakeWindow).get();
t.equal(params.wat, 'sup');
t.equal(params.foo, 'bar');
});
test('queryParams.update', function (t) {
t.plan(1);
var fakeWindow = {location: {hash: '#wat=sup', search: ''}};
var qp = new QueryParams(fakeWindow);
qp.update({foo: 'bar'});
t.equal(fakeWindow.location.hash, '#wat=sup&foo=bar');
});
Loading…
Cancel
Save