Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eslintとprettierを追加と、deposeを修正 #6

Closed
wants to merge 11 commits into from
Closed
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
lib
dist
71 changes: 71 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"extends": [
"airbnb",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:import/typescript",
"prettier",
"prettier/react"
],
"plugins": ["@typescript-eslint", "jest", "prettier"],
"parserOptions": {
"parser": "@typescript-eslint/parser"
},
"env": {
"browser": true,
"jest/globals": true
},
"rules": {
"prettier/prettier": ["error", {
"arrowParens": "always",
"printWidth": 140,
"singleQuote": true,
"trailingComma": "all",
"semi": false
}],
"import/prefer-default-export": "off",
"object-shorthand": ["error", "properties"],
"prefer-destructuring": ["error", {
"array": false,
"object": true
}, {
"enforceForRenamedProperties": false
}],
"@typescript-eslint/no-unused-vars": ["error", {
"vars": "all",
"args": "after-used",
"ignoreRestSiblings": false
}],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/camelcase": ["error", {
"properties": "never"
}],
"@typescript-eslint/member-delimiter-style": ["error", {
"multiline": {
"delimiter": "none",
"requireLast": false
},
"singleline": {
"delimiter": "semi",
"requireLast": false
}
}],
"no-underscore-dangle": ["error", {
"allowAfterThis": true
}],
"react/jsx-filename-extension": ["error", {
"extensions": [".jsx", ".tsx"]
}],
"react/destructuring-assignment": "off"
},
"overrides": [{
"files": ["*.ts", "*.tsx"],
"rules": {
"@typescript-eslint/no-var-requires": ["error"],
"@typescript-eslint/no-useless-constructor": [
"error"
]
}
}]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ lib/

# firebase
.firebaserc
firestore-debug.log

.expo
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
lib/
coverage/
node_modules/
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arrowParens": "always",
"printWidth": 140,
"singleQuote": true,
"trailingComma": "all",
"semi": false
}
45 changes: 45 additions & 0 deletions __tests__/command/message/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import uuid from 'uuid/v4'
import * as ftest from '@firebase/testing'
import { roomFactory } from '../../helper/factory/room'
import { sendTextMessage } from '../../../src/command/message'
import { getMessagePath, getRoomPath } from '../../../src/firebase/collectionSchema'
import { installApp } from '../../../src/firebase'
import { setMockUserForTest } from '../../../src/firebase/user'
import { setup, teardown } from '../../helper/setup'

const testName = 'unit-test-message-commands'
let db: firebase.firestore.Firestore
const sentFrom = { uid: uuid() }
const sentTo = { uid: uuid() }

function getRoomMockData() {
const room = roomFactory({ memberIds: [sentFrom.uid, sentTo.uid] })
return {
[`${getRoomPath()}/${room.id}`]: room,
}
}

const mockData = getRoomMockData()

describe(testName, () => {
beforeEach(async () => {
db = await setup({
auth: sentFrom,
data: mockData,
})
})

afterEach(async () => {
await teardown()
})

describe('sendTextMessage', () => {
test('success sendTextMessage', async () => {
installApp(db)
setMockUserForTest(sentFrom)
const room = mockData[Object.keys(mockData)[0]]
await sendTextMessage(room.id, { text: 'first test message' })
await ftest.assertSucceeds(db.collection(getMessagePath(room.id)).get())
})
})
})
19 changes: 19 additions & 0 deletions __tests__/helper/factory/room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as firebase from 'firebase/app'
import { Room, ROOM_TYPE } from '../../../src/domain/message/room'

let id = 0

type Base = Partial<Room> & { memberIds: Room['memberIds'] }

export function roomFactory<T extends Base>(overrides: T): Room & T {
id += 1
return {
id,
name: `${id}nth room`,
roomType: ROOM_TYPE.DIRECT,
memberIds: overrides.memberIds,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
updatedAt: firebase.firestore.FieldValue.serverTimestamp(),
...overrides,
}
}
46 changes: 46 additions & 0 deletions __tests__/helper/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as firebase from '@firebase/testing'
import fs from 'fs'
import appRoot from 'app-root-path'

const mockRules = fs.readFileSync(appRoot.resolve('firestore.mock.rules'), 'utf8')
const prodRules = fs.readFileSync(appRoot.resolve('firestore.rules'), 'utf8')

export async function setup<T extends { [key: string]: object }>({
auth,
data,
useRule,
}: {
auth?: { [key in 'uid' | 'email']?: string }
data?: T
useRule?: boolean
}) {
const projectId = `rules-spec-${Date.now()}`
const app = firebase.initializeTestApp({
projectId,
auth,
})

const db = app.firestore()

if (data) {
await firebase.loadFirestoreRules({
projectId,
rules: mockRules,
})

await Promise.all(Object.keys(data).map((path) => db.doc(path).set(data[path])))
}

if (typeof useRule === 'undefined' || useRule) {
await firebase.loadFirestoreRules({
projectId,
rules: prodRules,
})
}

return db
}

export async function teardown() {
Promise.all(firebase.apps().map((app) => app.delete()))
}
36 changes: 36 additions & 0 deletions __tests__/setup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { setup, teardown } from './helper/setup'

const mockUser = {
uid: 'jeffd23',
} as const

const mockData = {
'users/jeffd23': {
foo: 'bar',
},
'tasks/testTask': {
hello: 'world',
},
} as const

const testFunc = async () => {
const db = await setup({
auth: mockUser,
data: mockData,
useRule: false,
})

await Promise.all([
Object.keys(mockData).map(async (path) => {
const docSnap = await db.doc(path).get()
expect(docSnap.data() as any).toBe(mockData[path as keyof typeof mockData])
}),
])

await teardown()
}

describe('setup test', () => {
test('success setup1', testFunc)
test('success setup2', testFunc)
})
9 changes: 7 additions & 2 deletions example/expo/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */
import 'firebase/auth'
import 'firebase/firestore'
import * as firebase from 'firebase/app'
import config from './config.json'
import { installApp } from 'common-messanger'
import config from './config.json'
import Demo from './components/demo'

const app = firebase.initializeApp(config)
const firestore = firebase.firestore(app)
installApp(firestore)

firebase.auth().signInAnonymously().catch((error) => console.error(error));
firebase
.auth()
.signInAnonymously()
.catch((error) => console.error(error))

export default Demo
13 changes: 13 additions & 0 deletions example/expo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Install方法

```bash
$ yarn install && yarn build && cd example/expo && yarn install
```

### 解説

postinstallでコピーしているだけ
その理由は、
npm linkだとexpoがシンボリックリンク非サポートなのでできない

rsync使っている理由はcp -rだとnode_modulesが再起コピーされるので、rsyncのexcludeオプションを使うと楽だったから
Loading