-
Notifications
You must be signed in to change notification settings - Fork 7
Newsletter: signup on the About page, and a way back from the confirmation #1552
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
149 changes: 149 additions & 0 deletions
149
apps/self-hosted/src/features/blog/components/about-page-structure.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { readFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import ts from 'typescript'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| /** | ||
| * Source guards for the shape of the About page: it carries the signup, and it | ||
| * has a heading in every outcome. | ||
| * | ||
| * The page sits under a newsletter section whose own heading is an h2 | ||
| * (vision-web#1551). Three of the four shells carry a masthead h1 of their own, | ||
| * DefaultShell through BlogNavigation and journal and terminal directly, but | ||
| * the reader shell renders its title in a span. So on that template, any About | ||
| * outcome that skipped the page heading left the document starting at h2, which | ||
| * is exactly what the loading and failure branches used to do: they returned a | ||
| * bare line of text. | ||
| * | ||
| * Both variants now return through `AboutFrame`, which owns the h1, and the | ||
| * identity it needs comes from config rather than from the request. Checked in | ||
| * the source because the branch that regresses is a loading state behind a | ||
| * query, and the same approach as `failure-states.test.ts` next door. | ||
| */ | ||
|
|
||
| const FILE = join(__dirname, 'about-page.tsx'); | ||
| /** Every one of these must return through the frame, in every branch. */ | ||
| const VARIANTS = ['BlogAbout', 'CommunityAbout']; | ||
| const FRAME = 'AboutFrame'; | ||
|
|
||
| function parse(): ts.SourceFile { | ||
| return ts.createSourceFile( | ||
| FILE, | ||
| readFileSync(FILE, 'utf8'), | ||
| ts.ScriptTarget.Latest, | ||
| true, | ||
| ts.ScriptKind.TSX, | ||
| ); | ||
| } | ||
|
|
||
| function each(node: ts.Node, visit: (n: ts.Node) => void): void { | ||
| visit(node); | ||
| ts.forEachChild(node, (child) => each(child, visit)); | ||
| } | ||
|
|
||
| /** | ||
| * The return statements belonging to this function itself, not to callbacks | ||
| * nested inside it: a `useMemo(() => ...)` return is not what the component | ||
| * renders. | ||
| */ | ||
| function ownReturns(fn: ts.Node): ts.ReturnStatement[] { | ||
| const out: ts.ReturnStatement[] = []; | ||
| const walk = (node: ts.Node) => { | ||
| ts.forEachChild(node, (child) => { | ||
| if ( | ||
| ts.isArrowFunction(child) || | ||
| ts.isFunctionExpression(child) || | ||
| ts.isFunctionDeclaration(child) | ||
| ) { | ||
| return; | ||
| } | ||
| if (ts.isReturnStatement(child)) out.push(child); | ||
| walk(child); | ||
| }); | ||
| }; | ||
| walk(fn); | ||
| return out; | ||
| } | ||
|
|
||
| function functionNamed(source: ts.SourceFile, name: string): ts.Node { | ||
| let found: ts.Node | undefined; | ||
| each(source, (n) => { | ||
| if (ts.isFunctionDeclaration(n) && n.name?.text === name) found = n; | ||
| }); | ||
| if (!found) throw new Error(`${name} is no longer a function declaration`); | ||
| return found; | ||
| } | ||
|
|
||
| /** The tag name of a returned JSX element, or null for anything else. */ | ||
| function returnedTag(statement: ts.ReturnStatement): string | null { | ||
| const expr = statement.expression; | ||
| if (!expr) return null; | ||
| const jsx = ts.isParenthesizedExpression(expr) ? expr.expression : expr; | ||
| if (ts.isJsxElement(jsx)) return jsx.openingElement.tagName.getText(); | ||
| if (ts.isJsxSelfClosingElement(jsx)) return jsx.tagName.getText(); | ||
| return null; | ||
| } | ||
|
|
||
| describe('the About page carries the signup', () => { | ||
| const source = parse(); | ||
|
|
||
| /** | ||
| * Checked here rather than by rendering: AboutPage sits behind the router, | ||
| * the config store and a react-query provider, and what can regress is the | ||
| * mount itself. Rendering NewsletterSignup directly, which the component | ||
| * suite does, cannot see whether the page still mounts it. | ||
| */ | ||
| it('mounts it once, in the page frame, outside both variants', () => { | ||
| const body = functionNamed(source, 'AboutPage').getText(); | ||
| expect(body).toContain('<NewsletterSignup placement="page" />'); | ||
|
|
||
| // Outside the variant switch on purpose: both variants return early while | ||
| // their query is loading or failed, and the signup depends on neither. | ||
| for (const variant of VARIANTS) { | ||
| expect(functionNamed(source, variant).getText()).not.toContain( | ||
| 'NewsletterSignup', | ||
| ); | ||
| } | ||
|
|
||
| let mounts = 0; | ||
| each(source, (n) => { | ||
| if ( | ||
| ts.isJsxSelfClosingElement(n) && | ||
| n.tagName.getText() === 'NewsletterSignup' | ||
| ) { | ||
| mounts += 1; | ||
| } | ||
| }); | ||
| expect(mounts).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('the About page always has a heading', () => { | ||
| const source = parse(); | ||
|
|
||
| it.each(VARIANTS)('%s returns through the frame in every branch', (name) => { | ||
| // The component's OWN returns. Descending into nested functions would | ||
| // collect the `useMemo` callbacks' returns, which are not what renders. | ||
| const returns = ownReturns(functionNamed(source, name)); | ||
|
|
||
| // Loading, failed and success. If a branch is ever added, it is covered by | ||
| // the same assertion rather than needing a new case here. | ||
| expect(returns.length).toBeGreaterThanOrEqual(3); | ||
| expect(returns.map(returnedTag)).toEqual(returns.map(() => FRAME)); | ||
| }); | ||
|
|
||
| it('the frame is the one place the page heading lives', () => { | ||
| const text = readFileSync(FILE, 'utf8'); | ||
| // Exactly one, and it is inside the frame: two would give the page two | ||
| // titles in the success state, none would put us back where we started. | ||
| expect(text.match(/<h1[\s>]/g)?.length).toBe(1); | ||
|
|
||
| let headings = 0; | ||
| each(functionNamed(source, FRAME), (n) => { | ||
| if (ts.isJsxElement(n) && n.openingElement.tagName.getText() === 'h1') { | ||
| headings += 1; | ||
| } | ||
| }); | ||
| expect(headings).toBe(1); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.