Skip to content

Instantly share code, notes, and snippets.

@codegagan
codegagan / EffectComponent.test.js
Created August 30, 2020 08:14
Test the behavior defined in useEffect
import * as reactModule from "react";
import { shallow } from "enzyme";
import EffectComponent from "./EffectComponent";
describe("test App Component", () => {
it("should call the logic in useEffect", () => {
const setDataSize = jest.fn(size => {});
reactModule.useState = jest.fn(initialDataSize => [
initialDataSize,
setDataSize
@codegagan
codegagan / EffectComponent.js
Last active September 6, 2020 14:42
Test the behavior defined in useEffect
import React, { useState, useEffect } from "react";
export default function EffectComponent() {
const [dataSize, setDataSize] = useState(0);
const loadData = () => {
const dataFromApi = ['a', 'b', 'c'];
setDataSize(dataFromApi.length);
};
@codegagan
codegagan / MultipleStatesComponent.test.js
Created August 30, 2020 07:52
Test with multiple useState implementations
import * as reactModule from "react";
import { shallow } from "enzyme";
import MultipleStatesComponent from "./MultipleStatesComponent";
describe("test multiple state in component", () => {
let wrapper;
let setDataLength;
let setLoading;
let setText;
@codegagan
codegagan / MultipleStatesComponent.js
Last active September 6, 2020 14:42
Test with multiple useState implementations
import React, { useState } from "react";
export default function MultipleStatesComponent() {
const [dataLength, setDataLength] = useState(0);
const [loading, setLoading] = useState(false);
const [text, setText] = useState("");
const handleButtonOne = () => {
setDataLength(10);
};
@codegagan
codegagan / ConditionalComponent.test.js
Created August 30, 2020 07:20
Test conditional rendering based on state
import * as reactModule from "react";
import { shallow } from "enzyme";
import ConditionalComponent from "./ConditionalComponent";
describe("test Conditional component", () => {
it("should render while loading", () => {
const loadingValue = true;
reactModule.useState = jest.fn(initialLoadingValue => [
loadingValue,
() => {}
@codegagan
codegagan / ConditionalComponent.js
Last active September 6, 2020 14:42
Test conditional rendering based on state
import React, { useState } from "react";
export default function ConditionalComponent() {
const [loading, setLoading] = useState(false);
return (
<div>
<h1>Conditional test</h1>
{loading ? <p>Loading....</p> : <p>Page loaded successfully</p>}
</div>
@codegagan
codegagan / devSetup.sh
Last active August 9, 2020 10:42
ubuntu desktop developer setup
#!/bin/bash
#run sudo bash devSetup.sh
if [ "$EUID" -ne 0 ]
then echo "Please run as root/sudo "
exit 1
fi
@codegagan
codegagan / Soluton.java
Created May 20, 2019 05:33
Codility test problem find sum of two sub arrays of fixed length
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A, int K, int L) {
// write your code in Java SE 8
if(K + L > A.length){
@codegagan
codegagan / Solution.java
Created September 16, 2018 13:50
HackerRank Library system design in Java
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class Solution {
public static void main(String args[]) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
}
}
@codegagan
codegagan / TestClass.java
Created August 20, 2018 09:21
Tree Diameter problem
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;