Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

6.0 KB
Raw Blame
import { classSet, formatDate, timeSince, fileToBase64, sortTags } from "./utils"
import { readFileSync } from "fs"

// replaces non-breaking spaces with normal spaces
const normalize = (str: string) =>
  str
    .replace(/\xa0/g, " ")
    .replace(/\u202f/g, " ")
    .trim()

;[
  { input: null, expected: "" },
  { input: undefined, expected: "" },
  { input: {}, expected: "" },
  { input: { green: true }, expected: "green" },
  { input: { disabled: false, green: false }, expected: "" },
  { input: { disabled: true, green: true }, expected: "disabled green" },
  { input: { "ui button": true, green: true }, expected: "ui button green" },
  { input: { "ui button": true }, expected: "ui button" },
  { input: { "": true }, expected: "" },
  {
    input: {
      green: () => {
        throw Error()
      },
    },
    expected: "green",
  },
].forEach((x) => {
  test(`classSet of ${JSON.stringify(x.input)} should be ${x.expected}`, () => {
    const className = classSet(x.input)
    expect(className).toEqual(x.expected)
  })
})
;[
  { input: new Date(2018, 4, 27, 10, 12, 59), expectedEn: "May 27, 2018 at 10:12 AM", expectedPtBr: "27 de maio de 2018 às 10:12" },
  { input: new Date(2058, 12, 12, 23, 21, 53), expectedEn: "January 12, 2059 at 11:21 PM", expectedPtBr: "12 de janeiro de 2059 às 23:21" },
  { input: "2018-04-11T18:13:33.128082", expectedEn: "April 11, 2018 at 6:13 PM", expectedPtBr: "11 de abril de 2018 às 18:13" },
  { input: "2017-11-20T07:47:42.158142", expectedEn: "November 20, 2017 at 7:47 AM", expectedPtBr: "20 de novembro de 2017 às 07:47" },
].forEach((x) => {
  test(`[English] formatDate (full) of ${x.input} should be ${x.expectedEn}`, () => {
    const result = formatDate("en", x.input, "full")
    expect(normalize(result)).toEqual(x.expectedEn)
  })
  test(`[Brazilian Portuguese] formatDate (full) of ${x.input} should be ${x.expectedPtBr}`, () => {
    const result = formatDate("pt-BR", x.input, "full")
    expect(normalize(result)).toEqual(x.expectedPtBr)
  })
})
;[
  { input: new Date(2018, 4, 27, 10, 12, 59), expectedEn: "May 2018", expectedPtBr: "mai. de 2018" },
  { input: new Date(2058, 12, 12, 23, 21, 53), expectedEn: "Jan 2059", expectedPtBr: "jan. de 2059" },
  { input: "2018-04-11T18:13:33.128082", expectedEn: "Apr 2018", expectedPtBr: "abr. de 2018" },
  { input: "2017-11-20T07:47:42.158142", expectedEn: "Nov 2017", expectedPtBr: "nov. de 2017" },
].forEach((x) => {
  test(`[English] formatDate (short) of ${x.input} should be ${x.expectedEn}`, () => {
    const result = formatDate("en", x.input, "short")
    expect(normalize(result)).toEqual(x.expectedEn)
  })
  test(`[Brazilian Portuguese] formatDate (short) of ${x.input} should be ${x.expectedPtBr}`, () => {
    const result = formatDate("pt-BR", x.input, "short")
    expect(normalize(result)).toEqual(x.expectedPtBr)
  })
})
;[
  { input: new Date(2018, 4, 27, 10, 12, 59), expectedEn: "May 27, 2018", expectedPtBr: "27 de mai. de 2018" },
  { input: new Date(2058, 12, 12, 23, 21, 53), expectedEn: "Jan 12, 2059", expectedPtBr: "12 de jan. de 2059" },
  { input: "2018-04-11T18:13:33.128082", expectedEn: "Apr 11, 2018", expectedPtBr: "11 de abr. de 2018" },
  { input: "2017-11-20T07:47:42.158142", expectedEn: "Nov 20, 2017", expectedPtBr: "20 de nov. de 2017" },
].forEach((x) => {
  test(`[English] formatDate (date) of ${x.input} should be ${x.expectedEn}`, () => {
    const result = formatDate("en", x.input, "date")
    expect(normalize(result)).toEqual(x.expectedEn)
  })
  test(`[Brazilian Portuguese] formatDate (date) of ${x.input} should be ${x.expectedPtBr}`, () => {
    const result = formatDate("pt-BR", x.input, "date")
    expect(normalize(result)).toEqual(x.expectedPtBr)
  })
})
;[
  { input: new Date(2018, 4, 27, 19, 51, 9), expectedEn: "1 second ago", expectedPtBr: "há 1 segundo" },
  { input: new Date(2018, 4, 27, 10, 12, 59), expectedEn: "10 hours ago", expectedPtBr: "há 10 horas" },
  { input: new Date(2018, 4, 26, 10, 12, 59), expectedEn: "yesterday", expectedPtBr: "ontem" },
  { input: new Date(2018, 4, 22, 10, 12, 59), expectedEn: "5 days ago", expectedPtBr: "há 5 dias" },
  { input: new Date(2018, 3, 22, 10, 12, 59), expectedEn: "last month", expectedPtBr: "mês passado" },
  { input: new Date(2018, 2, 22, 10, 12, 59), expectedEn: "2 months ago", expectedPtBr: "há 2 meses" },
  { input: new Date(2017, 3, 22, 10, 12, 59), expectedEn: "last year", expectedPtBr: "ano passado" },
  { input: new Date(2013, 3, 22, 10, 12, 59), expectedEn: "5 years ago", expectedPtBr: "há 5 anos" },
].forEach((x) => {
  test(`[English] timeSince ${x.input} should be ${x.expectedEn}`, () => {
    const now = new Date(2018, 4, 27, 19, 51, 10)
    const result = timeSince("en", now, x.input)
    expect(normalize(result)).toEqual(x.expectedEn)
  })
  test(`[Brazilian Portuguese] timeSince ${x.input} should be ${x.expectedPtBr}`, () => {
    const now = new Date(2018, 4, 27, 19, 51, 10)
    const result = timeSince("pt-BR", now, x.input)
    expect(normalize(result)).toEqual(x.expectedPtBr)
  })
})

test("Can convert file to base64", async () => {
  const content = readFileSync("./favicon.png")
  const favicon = new File([content], "favicon.png")
  const base64 = await fileToBase64(favicon)
  expect(base64).toBe(Buffer.from(content).toString("base64"))
})

test("Can sort tags by not isPublic and then name", () => {
  const tags = [
    { name: "a", id: 1, color: "Blue", slug: "a", isPublic: true },
    { name: "b", id: 2, color: "Red", slug: "b", isPublic: false },
    { name: "ac", id: 3, color: "White", slug: "c", isPublic: true },
    { name: "d", id: 4, color: "Green", slug: "d", isPublic: false },
    { name: "e", id: 5, color: "Yellow", slug: "e", isPublic: true },
  ]
  const sortedTags = sortTags(tags)

  expect(sortedTags).toStrictEqual([
    { name: "b", id: 2, color: "Red", slug: "b", isPublic: false },
    { name: "d", id: 4, color: "Green", slug: "d", isPublic: false },
    { name: "a", id: 1, color: "Blue", slug: "a", isPublic: true },
    { name: "ac", id: 3, color: "White", slug: "c", isPublic: true },
    { name: "e", id: 5, color: "Yellow", slug: "e", isPublic: true },
  ])
})