Skip to content

Instantly share code, notes, and snippets.

View lfac-pt's full-sized avatar
:shipit:
Working

Luís Cardoso lfac-pt

:shipit:
Working
View GitHub Profile
@lfac-pt
lfac-pt / gist:5073918
Last active December 14, 2015 10:39
Snippet to add the page number to a reveal.js presentation. (Does not add a number to the first slide!)
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function currentPageFormatter(event) {
var formattedStr;
if (event.indexh === 0) {
return "";
}
formattedStr = event.indexh;
@lfac-pt
lfac-pt / Example 1.js
Last active May 29, 2019 09:55
Example 1 of my JavaScript and React unit tests basics
test("should update the dom", () => {
$("div").text("bob");
expect($("div").text()).toBe("bob");
});
@lfac-pt
lfac-pt / Example 2.js
Last active May 29, 2019 09:55
Example 2 of my JavaScript and React unit tests basics
describe("deletePost", () => {
test("calls the REST API method to delete the post, but only if the user is logged in", () => {
const mockUser = new User({ username: "bob" });
mockUser.ajax = spy();
mockUser.login();
mockUser.deletePost();
expect(mockUser.ajax).to.haveCallCount(1);
@lfac-pt
lfac-pt / Example 3.js
Created May 29, 2019 09:50
Example 3 of my JavaScript and React unit tests basics
describe("deletePost", () => {
let mockUser;
beforeEach(() => {
mockUser = new User({ username: "bob" });
mockUser.ajax = spy();
});
test("calls the REST API method to delete the post", () => {
@lfac-pt
lfac-pt / Example 4.js
Last active May 29, 2019 09:52
Example 4 of my JavaScript and React unit tests basics
describe("deletePost", () => {
test("calls the REST API method to delete the post", () => {
// Setup
const mockUser = new User({ username: "bob" });
mockUser.ajax = spy();
mockUser.login();
// Action
mockUser.deletePost();
@lfac-pt
lfac-pt / Example 5.js
Created May 29, 2019 09:53
Example 5 of my JavaScript and React unit tests basics
describe("searchPosts", () => {
test("filters the results according to the input search", () => {
const loadPostsBackup = User.prototype.loadPosts;
User.prototype.loadPosts = () => [{id: 1, title: "bob"}, {id: 2, title: "alice"}];
const mockUser = new User({ username: "bob" });
expect(mockUser.searchPosts("bob")).toEqual([{id: 1, title: "bob"}]);
@lfac-pt
lfac-pt / Example 6.js
Last active May 29, 2019 10:01
Example 6 of my JavaScript and React unit tests basics
describe("searchPosts", () => {
test("filters the results according to the input search", () => {
const mockUser = new User({ username: "bob" });
mockUser.loadPosts = () => [{id: 1, title: "bob"}, {id: 2, title: "alice"}];
expect(mockUser.searchPosts("bob")).toEqual([{id: 1, title: "bob"}]);
});
});
@lfac-pt
lfac-pt / Example 7.js
Created May 29, 2019 10:01
Example 7 of my JavaScript and React unit tests basics
<METHOD_NAME>
<TEST>
<TEST>
...
<METHOD_NAME>
<TEST>
<TEST>
...
@lfac-pt
lfac-pt / Example 8.js
Created May 29, 2019 10:03
Example 8 of my JavaScript and React unit tests basics
class Dialog extends Component {
deleteUser() {
// `deleteUser` implementation
}
render() {
// `render` implementation
}
}
@lfac-pt
lfac-pt / Example 9.js
Created May 29, 2019 10:06
Example 9 of my JavaScript and React unit tests basics
describe("deleteUser", () => {
test("makes a request to the delete user endpoint", () => {
// Test code
});
});
describe("render", () => {
test("renders the user name if the user is logged in", () => {
// Test code
});