-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
86 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,86 @@ | ||
import { FetchHttpClient } from '@effect/platform' | ||
import { test } from '@fast-check/jest' | ||
import { describe, expect } from '@jest/globals' | ||
import { Effect, pipe, TestContext } from 'effect' | ||
import fetchMock from 'fetch-mock' | ||
import { format } from 'fp-ts-routing' | ||
import { Status } from 'hyper-ts' | ||
import * as _ from '../src/about-us.js' | ||
import { DeprecatedSleepEnv, Locale } from '../src/Context.js' | ||
import { GhostApi } from '../src/ghost.js' | ||
import { aboutUsMatch } from '../src/routes.js' | ||
import * as fc from './fc.js' | ||
import { shouldNotBeCalled } from './should-not-be-called.js' | ||
|
||
describe('aboutUs', () => { | ||
test.prop([fc.supportedLocale(), fc.string({ unit: fc.alphanumeric(), minLength: 1 })])( | ||
'when the page can be loaded', | ||
async (locale, key) => { | ||
const fetch = fetchMock.sandbox().getOnce( | ||
{ | ||
url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14', | ||
query: { key }, | ||
}, | ||
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } }, | ||
) | ||
(locale, key) => | ||
Effect.gen(function* () { | ||
const fetch = fetchMock.sandbox().getOnce( | ||
{ | ||
url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14', | ||
query: { key }, | ||
}, | ||
{ body: { pages: [{ html: '<p>Foo<script>bar</script></p>' }] } }, | ||
) | ||
|
||
const actual = await _.aboutUs(locale)({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })() | ||
const actual = yield* pipe( | ||
_.aboutUs, | ||
Effect.provideService(FetchHttpClient.Fetch, fetch as typeof FetchHttpClient.Fetch.Service), | ||
) | ||
|
||
expect(actual).toStrictEqual({ | ||
_tag: 'PageResponse', | ||
canonical: format(aboutUsMatch.formatter, {}), | ||
current: 'about-us', | ||
status: Status.OK, | ||
title: expect.anything(), | ||
main: expect.anything(), | ||
skipToLabel: 'main', | ||
js: [], | ||
}) | ||
}, | ||
expect(actual).toStrictEqual({ | ||
_tag: 'PageResponse', | ||
canonical: format(aboutUsMatch.formatter, {}), | ||
current: 'about-us', | ||
status: Status.OK, | ||
title: expect.anything(), | ||
main: expect.anything(), | ||
skipToLabel: 'main', | ||
js: [], | ||
}) | ||
}).pipe( | ||
Effect.provideService(Locale, locale), | ||
Effect.provideService(DeprecatedSleepEnv, { sleep: shouldNotBeCalled }), | ||
Effect.provideService(GhostApi, { key }), | ||
Effect.provide(TestContext.TestContext), | ||
Effect.runPromise, | ||
), | ||
) | ||
|
||
test.prop([fc.supportedLocale(), fc.string({ unit: fc.alphanumeric(), minLength: 1 }), fc.fetchResponse()])( | ||
'when the page cannot be loaded', | ||
async (locale, key, response) => { | ||
const fetch = fetchMock.sandbox().getOnce( | ||
{ | ||
url: 'begin:https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14?', | ||
query: { key }, | ||
}, | ||
response, | ||
) | ||
async (locale, key, response) => | ||
Effect.gen(function* () { | ||
const fetch = fetchMock.sandbox().getOnce( | ||
{ | ||
url: 'begin:https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14?', | ||
query: { key }, | ||
}, | ||
response, | ||
) | ||
|
||
const actual = await _.aboutUs(locale)({ fetch, ghostApi: { key }, sleep: shouldNotBeCalled })() | ||
const actual = yield* pipe( | ||
_.aboutUs, | ||
Effect.provideService(FetchHttpClient.Fetch, fetch as typeof FetchHttpClient.Fetch.Service), | ||
) | ||
|
||
expect(actual).toStrictEqual({ | ||
_tag: 'PageResponse', | ||
status: Status.ServiceUnavailable, | ||
title: expect.anything(), | ||
main: expect.anything(), | ||
skipToLabel: 'main', | ||
js: [], | ||
}) | ||
expect(fetch.done()).toBeTruthy() | ||
}, | ||
expect(actual).toStrictEqual({ | ||
_tag: 'PageResponse', | ||
status: Status.ServiceUnavailable, | ||
title: expect.anything(), | ||
main: expect.anything(), | ||
skipToLabel: 'main', | ||
js: [], | ||
}) | ||
expect(fetch.done()).toBeTruthy() | ||
}).pipe( | ||
Effect.provideService(Locale, locale), | ||
Effect.provideService(DeprecatedSleepEnv, { sleep: shouldNotBeCalled }), | ||
Effect.provideService(GhostApi, { key }), | ||
Effect.provide(TestContext.TestContext), | ||
Effect.runPromise, | ||
), | ||
) | ||
}) |