Implement conditional filtering

This commit is contained in:
Robert Schäfer 2019-07-13 14:00:03 +02:00
parent 88ef6c00f2
commit f9969b964c
5 changed files with 64 additions and 9 deletions

View File

@ -79,6 +79,9 @@ type User {
input _UserFilter {
AND: [_UserFilter!]
OR: [_UserFilter!]
name_contains: String
about_contains: String
slug_contains: String
id: ID
id_not: ID
id_in: [ID!]

View File

@ -63,6 +63,7 @@
"date-fns": "2.0.0-beta.2",
"express": "~4.17.1",
"graphql": "~14.4.2",
"isemail": "^3.2.0",
"jsonwebtoken": "~8.5.1",
"linkify-it": "~2.1.0",
"nuxt": "~2.8.1",
@ -111,4 +112,4 @@
"vue-jest": "~3.0.4",
"vue-svg-loader": "~0.12.0"
}
}
}

View File

@ -16,7 +16,6 @@ describe('Users', () => {
$t: jest.fn(),
$apollo: {
loading: false,
mutate: jest.fn().mockResolvedValue(),
},
}
})
@ -33,5 +32,39 @@ describe('Users', () => {
wrapper = Wrapper()
expect(wrapper.is('div')).toBe(true)
})
describe('search', () => {
let searchAction
beforeEach(() => {
searchAction = (wrapper, { query }) => {
wrapper.find('input').setValue(query)
wrapper.find('form').trigger('submit')
return wrapper
}
})
describe('query looks like an email address', () => {
it('searches users for exact email address', async () => {
const wrapper = await searchAction(Wrapper(), { query: 'email@example.org' })
expect(wrapper.vm.email).toEqual('email@example.org')
expect(wrapper.vm.filter).toBe(null)
})
})
describe('query is just text', () => {
it('tries to find matching users by `name`, `slug` or `about`', async () => {
const wrapper = await searchAction(await Wrapper(), { query: 'Find me' })
const expected = {
OR: [
{ name_contains: 'Find me' },
{ slug_contains: 'Find me' },
{ about_contains: 'Find me' },
],
}
expect(wrapper.vm.email).toBe(null)
expect(wrapper.vm.filter).toEqual(expected)
})
})
})
})
})

View File

@ -63,6 +63,7 @@
<script>
import gql from 'graphql-tag'
import isemail from 'isemail'
export default {
data() {
@ -74,6 +75,7 @@ export default {
User: [],
hasNext: false,
email: null,
filter: null,
form: {
formData: {
query: '',
@ -127,9 +129,10 @@ export default {
`)
},
variables() {
const { offset, first, email } = this
const { offset, first, email, filter } = this
const variables = { first, offset }
if (email) variables.email = email
if (filter) variables.filter = filter
return variables
},
update({ User }) {
@ -150,7 +153,15 @@ export default {
submit(formData) {
this.offset = 0
const { query } = formData
this.email = query
if (isemail.validate(query)) {
this.email = query
this.filter = null
} else {
this.email = null
this.filter = {
OR: [{ name_contains: query }, { slug_contains: query }, { about_contains: query }],
}
}
},
},
}

View File

@ -6047,6 +6047,13 @@ isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=
isemail@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/isemail/-/isemail-3.2.0.tgz#59310a021931a9fb06bbb51e155ce0b3f236832c"
integrity sha512-zKqkK+O+dGqevc93KNsbZ/TqTUFd46MwWjYOoMrjIMZ51eU7DtQG3Wmd9SQQT7i7RVnuTPEiYEWHU3MSbxC1Tg==
dependencies:
punycode "2.x.x"
isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
@ -9173,16 +9180,16 @@ punycode@1.3.2:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
integrity sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=
punycode@2.x.x, punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
punycode@^1.2.4, punycode@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
integrity sha1-wNWmOycYgArY4esPpSachN1BhF4=
punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
q@^1.1.2:
version "1.5.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"