Skip to content

Instantly share code, notes, and snippets.

View amitgupta15's full-sized avatar

Amit Gupta amitgupta15

View GitHub Profile
it('should get render user details object', function () {
// Stub the get function
$myapp.get = function (url, callback) {
if (url === '/api/users?id=1') {
callback({ fname: 'Amit', lname: 'Gupta' });
} else {
callback(false);
}
};
$myapp.get = function (url, callback) {
var requestListener = function () {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = JSON.parse(httpRequest.response);
callback(response);
} else {
alert('There was a problem with the request');
}
}
it('should add a todo item to the list', function () {
var selector = document.querySelector('#selector');
selector.innerHTML = '<form id="aform"><input type="text" name="todo-input"><button>Submit</button></form><ul id="todo-list"></ul>';
var form = document.querySelector('#aform');
form.elements['todo-input'].value = 'task 1';
var ev = document.createEvent('HTMLEvents');
ev.initEvent('submit', true, true);
form.dispatchEvent(ev);
<body>
<form id="aform">
<input type="text" name="todo-input">
<button>Submit</button>
</form>
<ul id="todo-list"></ul>
<script src="app.js"></script>
</body>
@amitgupta15
amitgupta15 / js-fe-test-blog-todo.js
Created April 30, 2020 23:05
Javascript Testing
document.addEventListener('submit', function (event) {
event.preventDefault();
var elements = document.querySelector('#aform').elements;
var todoItem = elements['todo-input'].value;
var todoList = document.querySelector('#todo-list');
if (todoItem) {
todoList.innerHTML += '<li>' + todoItem + '</li>';
}
});
@amitgupta15
amitgupta15 / app.js
Last active April 30, 2020 21:46
Front End Test Blog - app.js
(function () {
'use strict';
// Create a global variable and expose it to the world
var $myapp = {};
self.$myapp = $myapp;
$myapp.isValidDate = function (dateString) {
var dateRegex = /(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/gi;
return dateRegex.test(dateString);
@amitgupta15
amitgupta15 / test.html
Last active April 30, 2020 20:57
A Test Runner
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Runner</title>
</head>
<body>
<div id="selector"></div>
function assert(isTrue) {
if (!isTrue) {
throw new Error();
}
}
@amitgupta15
amitgupta15 / it.js
Last active April 30, 2020 20:51
it-test-function
(function(){
'use strict';
/**
* test function
* @param {string} desc
* @param {function} fn
*/
function it(desc, fn) {
try {
@amitgupta15
amitgupta15 / final-search.html
Created February 24, 2020 22:05
Complete code listing
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search a List</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}