Begin adding tests for sanitizer

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-08-01 21:49:00 +01:00
parent 996221b221
commit 0e748d545e
6 changed files with 15 additions and 6 deletions

View File

@ -22,13 +22,13 @@ function cleanNrcc(input: string) { // Remove newlines and then <p> tags from in
export function removeNewlineAndPTag(input: string): string {
const regex = /[\n\r]|<\/?p[^>]*>/g;
return input.replace(regex, (match) => {
return input.replace(regex, function(match) {
if (match === "\n" || match === "\r") {
return "";
} else {
return "";
}
})
});
}
async function getDomainFromEmail(mail: string) { // Needs testing

View File

@ -13,7 +13,7 @@ export function transform(input: any): StaffLdb | null {
generatedAt: transformDateTime(data?.generatedAt) || new Date(),
locationName: data?.locationName || "Not Found",
stationManagerCode: data?.stationManagerCode || "UK",
nrccMessages: transformNrcc(data?.nrccMessages),
nrccMessages: transformNrcc(data?.nrccMessages) || undefined,
trainServices: transformTrainServices(data?.trainServices),
busServices: transformTrainServices(data?.busServices),
ferryServices: transformTrainServices(data?.ferryServices)
@ -32,7 +32,7 @@ function transformDateTime(input: string): Date {
function transformNrcc(input: any): NrccMessage[] {
let output: NrccMessage[] = []
if (!Array.isArray(input?.message)) {
input.message = [input.message]
input.message = [input?.message]
}
for (const item of input?.message) {
let message: NrccMessage = {

View File

@ -0,0 +1,9 @@
import { removeNewlineAndPTag } from "../../src/utils/sanitizer.utils";
describe('sanitizer', () => {
test('Should remove /\n and <p>/</p> elements', () => {
const input = "\n<p>This is a string</p>";
const expectedOutput = "This is a string"
expect(removeNewlineAndPTag(input)).toEqual(expectedOutput);
});
});

View File

@ -1,7 +1,7 @@
import { transform } from "../../../../src/utils/translators/ldb/staffStation";
import { inputs } from "./stationInputs.test.data";
import { outputs } from "./stationOutputs.test.data";
import { inputs } from "./stationInputs";
import { outputs } from "./stationOutputs";
describe('transform', () => {
test('Should return null for empty input', () => {