Skip to content

Commit

Permalink
feat(core): update testing and add query url
Browse files Browse the repository at this point in the history
What's New:
- Implementing testing
- add support setFiltered, setSorted and setQuery
  • Loading branch information
chornos13 committed Dec 18, 2020
1 parent 863bae9 commit db7130d
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 42 deletions.
7 changes: 7 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// jest.config.ts

export default async (): Promise<any> => {
return {
moduleDirectories: ['node_modules', 'src'],
}
}
112 changes: 86 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"yup": "^0.29.1"
},
"devDependencies": {
"@types/jest": "^26.0.19",
"@types/lodash": "^4.14.159",
"@types/node": "^14.0.6",
"@types/react": "^16.9.35",
Expand All @@ -81,9 +82,10 @@
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.18.0",
"eslint-plugin-react-hooks": "^4.0.8",
"jest": "^26.1.0",
"jest": "^26.6.3",
"prettier": "^1.19.1",
"standard-version": "^9.0.0",
"ts-node": "^9.1.1",
"typescript": "^3.9.3"
},
"engines": {
Expand Down
19 changes: 15 additions & 4 deletions src/data/useFaq.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useQuery, UseQueryOptions } from 'react-query'
import ApiCall from 'services/ApiCall'
import useUrlQuery from 'helpers/QueryUrl/useUrlQuery'

interface FaqData {
owner: {
Expand All @@ -8,17 +10,26 @@ interface FaqData {
}

function useFaq(options?: UseQueryOptions<FaqData>) {
const queryUrl = useUrlQuery()

const query = useQuery<FaqData>(
'/common/faq',
queryUrl.transformKey('/common/faq'),
() =>
fetch(
'https://github.com/gitapi/repos/chornos13/nextjs-concept',
).then((res) => res.json()),
ApiCall.api
.get(
queryUrl.transformUrl(
'https://github.com/gitapi/repos/chornos13/nextjs-concept?',
),
)
.then((res) => {
return res.data
}),
options,
)

return {
...query,
helper: queryUrl,
}
}

Expand Down
71 changes: 71 additions & 0 deletions src/helpers/QueryUrl/ArrayQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
interface ArrayQueryOptions {
keyId?: string
keyValue?: string
filterValue?: {
only?: any[]
exclude?: any[]
}
}

interface Queries {
[key: string]: any
}

class ArrayQuery {
private keyId: string

private keyValue: string

private options: ArrayQueryOptions

private queries: Queries[] = []

constructor(options?: ArrayQueryOptions) {
this.options = options || {}

const { keyId = 'id', keyValue = 'value' } = this.options
this.keyId = keyId
this.keyValue = keyValue
}

count(id: string) {
return this.getQueries().filter((q) => q[this.keyId] === id).length
}

setQuery(id: string, value: any) {
const { filterValue: { exclude, only } = {} } = this.options
// prevent duplication
this.remove(id)

if (only && !only.includes(value)) {
return
}

if (exclude && exclude.includes(value)) {
return
}

this.queries.push({
[this.keyId]: id,
[this.keyValue]: value,
})
}

getQueries() {
return this.queries
}

find(id: string) {
return this.queries.find((query) => query[this.keyId] === id)
}

remove(id: string) {
this.queries = this.queries.filter((query) => query[this.keyId] !== id)
}

stringify() {
return JSON.stringify(this.queries)
}
}

export default ArrayQuery
27 changes: 27 additions & 0 deletions src/helpers/QueryUrl/QueryUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ArrayQuery from 'helpers/QueryUrl/ArrayQuery'

class QueryUrl {
public filtered: ArrayQuery

public sorted: ArrayQuery

constructor() {
this.filtered = new ArrayQuery({
keyId: 'id',
keyValue: 'value',
filterValue: {
exclude: [undefined, ''],
},
})

this.sorted = new ArrayQuery({
keyId: 'id',
keyValue: 'desc',
filterValue: {
only: [true, false],
},
})
}
}

export default QueryUrl
Loading

0 comments on commit db7130d

Please sign in to comment.