Skip to content

Commit

Permalink
test: make property tests easier to read
Browse files Browse the repository at this point in the history
This change uses the dedicated fast-check library for Jest, which reduces the
amount of code required. The main benefit is that there's not so much
indentation, so it's easier to read.

Refs https://github.com/dubzzz/fast-check/tree/main/packages/jest
  • Loading branch information
thewilkybarkid committed Feb 17, 2023
1 parent ff86907 commit 2ea9bea
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 118 deletions.
99 changes: 76 additions & 23 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"devDependencies": {
"@commitlint/cli": "^16.2.3",
"@commitlint/config-conventional": "^16.2.1",
"@fast-check/jest": "^1.6.0",
"@trivago/prettier-plugin-sort-imports": "^3.2.0",
"@tsconfig/recommended": "^1.0.1",
"@types/doi-regex": "^0.1.0",
Expand All @@ -31,7 +32,6 @@
"eslint-import-resolver-typescript": "^2.7.1",
"eslint-plugin-import": "^2.26.0",
"expect-type": "^0.13.0",
"fast-check": "^2.24.0",
"fp-ts": "^2.11.9",
"husky": "^7.0.4",
"jest": "^29.4.3",
Expand Down
136 changes: 42 additions & 94 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,137 +1,85 @@
import { describe, expect, test } from '@jest/globals'
import { test } from '@fast-check/jest'
import { describe, expect } from '@jest/globals'
import * as O from 'fp-ts/Option'
import * as _ from '../src'
import * as fc from './fc'

describe('doi-ts', () => {
describe('destructors', () => {
test('toUrl', () => {
fc.assert(
fc.property(fc.doi(), doi => {
expect(_.toUrl(doi)).toStrictEqual(new URL(`https://doi.org/${doi}`))
}),
)
test.prop([fc.doi()])('toUrl', doi => {
expect(_.toUrl(doi)).toStrictEqual(new URL(`https://doi.org/${doi}`))
})
})

describe('instances', () => {
describe('Eq', () => {
test('with the same DOI', () => {
fc.assert(
fc.property(fc.doi(), doi => {
expect(_.Eq.equals(doi, doi)).toBe(true)
}),
)
test.prop([fc.doi()])('with the same DOI', doi => {
expect(_.Eq.equals(doi, doi)).toBe(true)
})

test('with the same DOI in different cases', () => {
fc.assert(
fc.property(fc.doi(), doi => {
expect(_.Eq.equals(doi, doi.toLowerCase() as _.Doi)).toBe(true)
}),
)
test.prop([fc.doi()])('with the same DOI in different cases', doi => {
expect(_.Eq.equals(doi, doi.toLowerCase() as _.Doi)).toBe(true)
})

test('with different DOIs', () => {
fc.assert(
fc.property(fc.doi(), fc.doi(), (doi1, doi2) => {
expect(_.Eq.equals(doi1, doi2)).toBe(false)
}),
)
test.prop([fc.doi(), fc.doi()])('with different DOIs', (doi1, doi2) => {
expect(_.Eq.equals(doi1, doi2)).toBe(false)
})
})
})

describe('refinements', () => {
describe('isDoi', () => {
test('with a DOI', () => {
fc.assert(
fc.property(fc.doi(), doi => {
expect(_.isDoi(doi)).toBe(true)
}),
)
test.prop([fc.doi()])('with a DOI', doi => {
expect(_.isDoi(doi)).toBe(true)
})

test('with a non-DOI', () => {
fc.assert(
fc.property(fc.anything(), value => {
expect(_.isDoi(value)).toBe(false)
}),
)
test.prop([fc.anything()])('with a non-DOI', value => {
expect(_.isDoi(value)).toBe(false)
})
})

describe('hasRegistrant', () => {
test('when the registrant matches', () => {
fc.assert(
fc.property(
fc
.array(fc.registrant(), { minLength: 1 })
.chain(registrants => fc.tuple(fc.constant(registrants), fc.doi(fc.constantFrom(...registrants)))),
([registrants, doi]) => {
expect(_.hasRegistrant(...registrants)(doi)).toBe(true)
},
),
)
test.prop([
fc
.array(fc.registrant(), { minLength: 1 })
.chain(registrants => fc.tuple(fc.constant(registrants), fc.doi(fc.constantFrom(...registrants)))),
])('when the registrant matches', ([registrants, doi]) => {
expect(_.hasRegistrant(...registrants)(doi)).toBe(true)
})

test('when the registrant does not match', () => {
fc.assert(
fc.property(fc.array(fc.registrant()), fc.doi(), (registrants, doi) => {
expect(_.hasRegistrant(...registrants)(doi)).toBe(false)
}),
)
test.prop([fc.array(fc.registrant()), fc.doi()])('when the registrant does not match', (registrants, doi) => {
expect(_.hasRegistrant(...registrants)(doi)).toBe(false)
})
})
})

describe('utils', () => {
describe('parse', () => {
test('when it contains a DOI', () => {
fc.assert(
fc.property(
fc
.tuple(
fc.doi(),
fc.stringOf(fc.constant(' ')),
fc.constantFrom(
'doi:',
'https://doi.org/',
'http://doi.org/',
'https://dx.doi.org/',
'http://dx.doi.org/',
),
fc.stringOf(fc.constant(' ')),
)
.map(([doi, whitespaceBefore, prefix, whitespaceAfter]) => [
doi,
`${whitespaceBefore}${prefix}${doi}${whitespaceAfter}`,
]),
([doi, input]) => {
expect(_.parse(input)).toStrictEqual(O.some(doi))
},
),
)
test.prop([
fc
.tuple(
fc.doi(),
fc.stringOf(fc.constant(' ')),
fc.constantFrom('doi:', 'https://doi.org/', 'http://doi.org/', 'https://dx.doi.org/', 'http://dx.doi.org/'),
fc.stringOf(fc.constant(' ')),
)
.map(([doi, whitespaceBefore, prefix, whitespaceAfter]) => [
doi,
`${whitespaceBefore}${prefix}${doi}${whitespaceAfter}`,
]),
])('when it contains a DOI', ([doi, input]) => {
expect(_.parse(input)).toStrictEqual(O.some(doi))
})

test('when it does not contain a DOI', () => {
fc.assert(
fc.property(fc.unicodeString(), input => {
expect(_.parse(input)).toStrictEqual(O.none)
}),
)
test.prop([fc.unicodeString()])('when it does not contain a DOI', input => {
expect(_.parse(input)).toStrictEqual(O.none)
})
})

test('getRegistrant', () => {
fc.assert(
fc.property(
fc.registrant().chain(registrant => fc.tuple(fc.constant(registrant), fc.doi(fc.constant(registrant)))),
([registrant, doi]) => {
expect(_.getRegistrant(doi)).toBe(registrant)
},
),
)
test.prop([
fc.registrant().chain(registrant => fc.tuple(fc.constant(registrant), fc.doi(fc.constant(registrant)))),
])('getRegistrant', ([registrant, doi]) => {
expect(_.getRegistrant(doi)).toBe(registrant)
})
})
})

0 comments on commit 2ea9bea

Please sign in to comment.