Skip to content

Commit

Permalink
add a test for readme example
Browse files Browse the repository at this point in the history
* which currently fails => need to update code..
  • Loading branch information
Aprillion committed Dec 9, 2020
1 parent 9cefcc9 commit 9b8831f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"scripts": {
"clean": "rimraf ./lib",
"build": "yarn clean && tsc",
"test": "jest",
"prepublishOnly": "yarn build"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions src/LiveStorage.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LiveStorage } from './LiveStorage'

test('REAME example', () => {
const scenario = () => {
const posts = new LiveStorage('posts', [] as any[])
posts.update((prevPosts) => prevPosts.concat({ title: 'Brave new world' }))

expect(posts.value).toEqual([{ title: 'Brave new world' }])
}
expect(scenario).not.toThrow()
})
5 changes: 4 additions & 1 deletion src/LiveStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ interface BroadcastChannelMessageType<ValueType> {
}
}

const IS_BROWSER = typeof window !== 'undefined'
// Jest jsdom contains `window` object in Node => detect required features
const IS_BROWSER =
typeof BroadcastChannel !== 'undefined' &&
typeof sessionStorage !== 'undefined'

export class LiveStorage<ValueType> {
public id: string
Expand Down

0 comments on commit 9b8831f

Please sign in to comment.