Last active
February 8, 2021 21:03
-
-
Save abiodun0/0da43b1d51a36f2e187ed4fe37f63cd4 to your computer and use it in GitHub Desktop.
Testing adhoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getGithubURL(resource) { | |
return `https://github.com/${resource}`; | |
} | |
function updateStoryViewWidth() { | |
const storyViewElem = document.querySelector('.story-view'); | |
const { height } = storyViewElem.getBoundingClientRect(); | |
storyViewElem.style.width = `${height / 1.77}px`; | |
} | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = { | |
getGithubURL, | |
updateStoryViewWidth | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const it = (name, fn) => { | |
console.log('>', name); | |
fn() | |
} | |
const describe = (name, fn) => { | |
console.log(`=====${name} block====`) | |
fn(); | |
} | |
const assert = (cond, desc) => { | |
if (cond) { | |
return console.log('✔️', desc) | |
} | |
return console.assert(cond, desc) | |
} | |
function getGithubURL(resource) { | |
return `https://github.com/${resource}`; | |
} | |
function updateStoryViewWidth() { | |
const storyViewElem = document.querySelector('.story-view'); | |
console.log(storyViewElem, 'what?') | |
const { height } = storyViewElem.getBoundingClientRect(); | |
storyViewElem.style.width = `${height / 1.77}px`; | |
} | |
describe('Utility functions', () => { | |
it("returns right url", () => { | |
const someName = 'somename' | |
const result = getGithubURL(someName) | |
assert(result.includes('github'), "github") | |
}); | |
it("failed it returns right url", () => { | |
const someName = 'somename' | |
const result = getGithubURL(someName) | |
assert(result.includes('xxx'), "github") | |
}); | |
it("test mocking", () => { | |
let callTimes = 0; | |
global.document = { | |
querySelector: () => { | |
return { | |
getBoundingClientRect: () => { | |
callTimes +=1 | |
return { | |
hieght: '77' | |
} | |
}, | |
style: { | |
width: 22 | |
} | |
} | |
} | |
} | |
updateStoryViewWidth() | |
assert(callTimes === 1, "mock calls") | |
}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require("assert"); | |
const someFunc = require("./some_func"); | |
const { getGithubURL, updateStoryViewWidth } = someFunc; | |
const it = (name, fn) => { | |
try { | |
fn(); | |
console.log("✅", name); | |
} catch (e) { | |
console.log(`❌`, name); | |
console.log(e.stack); | |
} | |
}; | |
const describe = (name, fn) => { | |
console.log(`=====${name} block====`); | |
fn(); | |
}; | |
describe("Utility functions", () => { | |
it("returns right url", () => { | |
const someName = "somename"; | |
const result = getGithubURL(someName); | |
assert.ok(result.includes("github")) | |
}); | |
it("failed it returns right url", () => { | |
const someName = "somename"; | |
const result = getGithubURL(someName); | |
assert.ok(result.includes("xxx")) | |
}); | |
it("test mocking", () => { | |
// This is a poor mans spy/mock library impklementation | |
const tracker = new assert.CallTracker(); | |
const getBoundRectMock = () => ({hiehgt: 77}) | |
const querySelectormock = () => | |
({ | |
getBoundingClientRect: getBoundingClientRectCalls, | |
style: { | |
widht: 22 | |
} | |
}) | |
const querySelectCalls = tracker.calls(querySelectormock, 1); | |
const getBoundingClientRectCalls = tracker.calls(getBoundRectMock, 1); | |
// Mock the global DOM element | |
global.document = { | |
querySelector: querySelectCalls | |
}; | |
// Call the mutating DOM function | |
updateStoryViewWidth(); | |
// Verify that it's called the exact number of times | |
tracker.verify() | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ok } = require('assert'); | |
const someFunc = require('./some_func') | |
const { getGithubURL, updateStoryViewWidth } = someFunc | |
const it = (name, fn) => { | |
console.log('>>>>', name); | |
fn() | |
} | |
const describe = (name, fn) => { | |
console.log(`=====${name} block====`) | |
fn(); | |
} | |
const assert = (cond, desc) => { | |
try { | |
ok(cond) | |
console.log('✅', desc) | |
} catch (e) { | |
console.log(`❌`, desc) | |
console.log(e.stack) | |
} | |
} | |
describe('Utility functions', () => { | |
it("returns right url", () => { | |
const someName = 'somename' | |
const result = getGithubURL(someName) | |
assert(result.includes('github'), "github") | |
}); | |
it("failed it returns right url", () => { | |
const someName = 'somename' | |
const result = getGithubURL(someName) | |
assert(result.includes('xxx'), "github") | |
}); | |
it("test mocking", () => { | |
let callTimes = 0; | |
global.document = { | |
querySelector: () => { | |
return { | |
getBoundingClientRect: () => { | |
callTimes += 1 | |
return { | |
hieght: '77' | |
} | |
}, | |
style: { | |
width: 22 | |
} | |
} | |
} | |
} | |
updateStoryViewWidth() | |
assert(callTimes === 1, "mock calls") | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment