generated from ministryofjustice/hmpps-template-typescript
-
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.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
server/views/components/expandableComment/expandableComment.test.ts
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import nunjucks, { Template } from 'nunjucks' | ||
import * as cheerio from 'cheerio' | ||
import { registerNunjucks } from '../../../utils/nunjucksSetup' | ||
|
||
describe('Expandable comment component', () => { | ||
const njkEnv = registerNunjucks() | ||
const templateString = ` | ||
{%- from "components/expandableComment/macro.njk" import bapvExpandableComment -%} | ||
{{- bapvExpandableComment(comment) -}}` | ||
|
||
let compiledTemplate: Template | ||
let viewContext: Record<string, unknown> | ||
|
||
beforeEach(() => { | ||
viewContext = {} | ||
}) | ||
|
||
it('should handle missing input', () => { | ||
compiledTemplate = nunjucks.compile(templateString, njkEnv) | ||
const $ = cheerio.load(compiledTemplate.render(viewContext)) | ||
expect($('body').html()).toBe('') | ||
}) | ||
|
||
it('should trim input and not render additional markup if no newline within the text', () => { | ||
viewContext = { | ||
comment: { text: ' \na string with no newline in the text \r\n' }, | ||
} | ||
compiledTemplate = nunjucks.compile(templateString, njkEnv) | ||
const $ = cheerio.load(compiledTemplate.render(viewContext)) | ||
expect($('body').html().trim()).toBe('a string with no newline in the text') | ||
}) | ||
|
||
it('should split on first newline and add expandable comment markup', () => { | ||
viewContext = { | ||
comment: { text: 'line one\nline two\nline three' }, | ||
} | ||
compiledTemplate = nunjucks.compile(templateString, njkEnv) | ||
const $ = cheerio.load(compiledTemplate.render(viewContext)) | ||
|
||
expect($('.bapv-expandable-comment').length).toBe(1) | ||
expect($('.bapv-expandable-comment__first-line').text()).toBe('line one') | ||
expect($('.bapv-expandable-comment__show').length).toBe(1) | ||
expect($('.bapv-expandable-comment__full-comment').text()).toBe('line two\nline three') | ||
expect($('.bapv-expandable-comment__hide').length).toBe(1) | ||
}) | ||
}) |