Skip to content

Instantly share code, notes, and snippets.

View nanlabsweb's full-sized avatar

nanlabsweb

View GitHub Profile
@nanlabsweb
nanlabsweb / hello_world.cc
Created March 13, 2018 21:45
JS native extensions example - C code
#include <napi.h>
Napi::String SayHi(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, "Hi!");
}
Napi::Object init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "sayHi"), Napi::Function::New(env, SayHi));
@nanlabsweb
nanlabsweb / graphql-query.js
Created March 13, 2018 01:14
GraphQL query basic example.
{
pages {
edges {
node {
id
title
date
author {
username
}
@nanlabsweb
nanlabsweb / People.js
Created March 8, 2018 13:48
People container for redux-thunk demo.
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { fetchPeople } from './peopleActions';
import PeopleList from './components/PeopleList';
import Loader from '../common/loader';
class People extends Component {
@nanlabsweb
nanlabsweb / loadingReducer.js
Created March 8, 2018 13:47
Loading Reducer for redux-thunk demo.
const loadingReducer = (state = false, action) => {
switch (action.type) {
case 'FETCH_PEOPLE_HAS_STARTED':
return true;
case 'FETCH_PEOPLE_HAS_FINISHED':
return false;
default:
return state;
}
}
@nanlabsweb
nanlabsweb / peopleReducer.js
Created March 8, 2018 13:46
People Reducer for redux-thunk demo.
import {
FETCH_PEOPLE_HAS_FINISHED,
} from './peopleActions';
const peopleReducer = (state = [], action) => {
switch (action.type) {
case FETCH_PEOPLE_HAS_FINISHED:
return action.data;
default:
return state;
@nanlabsweb
nanlabsweb / peopleActions.js
Created March 8, 2018 13:46
Actions file for redux-thunk demo.
export const FETCH_PEOPLE_HAS_STARTED = 'FETCH_PEOPLE_HAS_STARTED';
export const FETCH_PEOPLE_HAS_FINISHED = 'FETCH_PEOPLE_HAS_FINISHED';
const listOfPeople = [
{
id: 1,
name: 'Max',
age: 21,
}, {
id: 2,
@nanlabsweb
nanlabsweb / store.js
Created March 8, 2018 13:44
Store file for redux-thunk post.
import { createStore, applyMiddleware, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import peopleReducer from '../people/peopleReducer';
import loadingReducer from './loadingReducer';
const reducers = combineReducers({
people: peopleReducer,
loading: loadingReducer,
});
@nanlabsweb
nanlabsweb / ItemCreationUnitTests.kt
Created February 5, 2018 14:28
Wrapping externals to draw clear architectural boundaries.
// ...
"when user is not authenticated get sign in intent" {
val authenticator = Mockito.mock(Authenticator::class.java)
`when`(authenticator.isAuthenticated()).thenReturn(false)
val context = Mockito.mock(Context::class.java)
val authUI = Mockito.mock(AuthUIWrapper::class.java)
val generator = ActionIntentGenerator(context, authUI)
val generatorSpy = Mockito.spy(generator)
@nanlabsweb
nanlabsweb / ItemCreationUnitTests.kt
Created February 5, 2018 14:26
Using simple factories to assert proper interaction between modules.
// ...
"ItemViewModel should insert data in the item repository" {
val repository = Mockito.mock(ItemRepository::class.java)
val item = Item(...properties...) // replace by actual properties
val itemFactory = Mockito.mock(ItemFactory::class.java)
`when`(itemFactory.build(...properties..)).thenReturn(item) // replace by actual properties
val transformedItem = // do whatever transformation you expect the test subject to do
// the test subject is the itemViewModel
@nanlabsweb
nanlabsweb / MainActivity.kt
Created February 5, 2018 14:23
Activity used only for presentation.
class MainActivity : AppCompatActivity(), KoinComponent {
private val itemViewModel by inject<ItemViewModel>()
private val itemAdapter by inject<ItemAdapter>()
private val authViewModel by inject<AuthViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpAddButtonBehavior()