Skip to content

Instantly share code, notes, and snippets.

@relekang
Created March 15, 2016 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relekang/e2f82f2670cf714752ef to your computer and use it in GitHub Desktop.
Save relekang/e2f82f2670cf714752ef to your computer and use it in GitHub Desktop.
API mock with local_storage
import _ from 'lodash'
export const get = (path, data) => {
const collection = JSON.parse(localStorage.getItem(path)) || []
if (data && data.id) {
return Promise.resolve(_.find(collection, ['id', data.id]))
}
return Promise.resolve(collection)
}
export const add = (path, data) => {
const collection = JSON.parse(localStorage.getItem(path)) || []
const item = Object.assign({}, data, { id: collection.length + 1 })
localStorage.setItem(path, JSON.stringify([...collection, item]))
return Promise.resolve(item)
}
export const save = (path, data) => {
const collection = JSON.parse(localStorage.getItem(path)) || []
const index = _.findIndex(collection, ['id', data.id])
const item = Object.assign({}, collection[index], data)
localStorage.setItem(path, JSON.stringify([
...collection.slice(0, index),
item,
...collection.slice(index + 1),
]))
return Promise.resolve(item)
}
export const update = (path, data) => save(path, data)
export const remove = (path, { id }) => {
const collection = JSON.parse(localStorage.getItem(path)) || []
const index = _.findIndex(collection, ['id', id])
const item = collection[index]
localStorage.setItem(path, JSON.stringify([
...collection.slice(0, index),
...collection.slice(index + 1),
]))
return Promise.resolve(item)
}
import 'mock-local-storage'
import test from 'ava'
import { add, get, save } from '../src/local_storage'
test.beforeEach(() => localStorage.clear())
test.serial('get all', t => {
t.plan(1)
const data = [{ title: 'wat' }]
localStorage.setItem('/coffees', JSON.stringify(data))
return get('/coffees')
.then(response => t.same(response, data))
})
test.serial('get single', t => {
t.plan(1)
const item = { title: 'a title', id: 2 }
const data = [{ title: 'wat', id: 42 }, item]
localStorage.setItem('/coffees', JSON.stringify(data))
return get('/coffees', { id: 2 })
.then(response => t.same(response, item))
})
test.serial('add', t => {
t.plan(2)
const data = [{ title: 'wat', id: 42 }]
localStorage.setItem('/coffees', JSON.stringify(data))
return add('/coffees', { title: 'a title' })
.then(response => {
t.same(response, { title: 'a title', id: 2 })
t.same(JSON.parse(localStorage.getItem('/coffees')), [...data, { title: 'a title', id: 2 }])
})
})
test.serial('save', t => {
t.plan(2)
const data = [{ title: 'wat', id: 42 }, { title: 'wat', id: 5 }]
localStorage.setItem('/coffees', JSON.stringify(data))
return save('/coffees', { title: 'a title', id: 5 })
.then(response => {
t.same(response, { title: 'a title', id: 5 })
t.same(JSON.parse(localStorage.getItem('/coffees')), [data[0], { title: 'a title', id: 5 }])
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment