Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

3.5 KB
Raw Blame
import { displayName, emailChanged, nameInitial, onlyLetterCaseChanged } from './profile'

describe('displayName', () => {
  it('prefers the name the person chose', () => {
    expect(displayName('Carter', '[email protected]')).toBe('Carter')
  })

  it('falls back to the local part of the email when no name is set', () => {
    expect(displayName('', '[email protected]')).toBe('carter')
    expect(displayName(undefined, '[email protected]')).toBe('carter')
  })

  // A row written before the server trimmed, or a settings payload from an older
  // instance; either way the greeting must not open with a space.
  it('trims a stored name, and treats a whitespace-only one as unset', () => {
    expect(displayName('  Carter  ', '[email protected]')).toBe('Carter')
    expect(displayName('   ', '[email protected]')).toBe('carter')
  })

  // The empty-user window while the first /me is in flight.
  it('reads as a sentence when there is neither name nor email', () => {
    expect(displayName(undefined, undefined)).toBe('there')
    expect(displayName('', '')).toBe('there')
  })

  it('keeps a name that happens to contain an @', () => {
    expect(displayName('@carter', '[email protected]')).toBe('@carter')
  })
})

describe('nameInitial', () => {
  it('is the first letter of whatever displayName returned', () => {
    expect(nameInitial(displayName('Carter', '[email protected]'))).toBe('C')
    expect(nameInitial(displayName('', '[email protected]'))).toBe('C')
  })

  // Not every name starts with a Latin letter, and toUpperCase must not mangle one.
  it('leaves a non-cased first character alone', () => {
    expect(nameInitial('日本')).toBe('日')
  })

  // The name is free text, so it can start outside the BMP. charAt(0) would return half
  // a surrogate pair here and the avatar would render a replacement glyph.
  it('takes a whole code point, not half a surrogate pair', () => {
    expect(nameInitial('🦍 Carter')).toBe('🦍')
    expect(nameInitial('🦍 Carter')).toHaveLength(2) // one code point, two UTF-16 units
  })
})

describe('onlyLetterCaseChanged', () => {
  it('is true when only ASCII letter case differs', () => {
    expect(onlyLetterCaseChanged('[email protected]', '[email protected]')).toBe(true)
    expect(onlyLetterCaseChanged(' [email protected] ', '[email protected]')).toBe(true)
  })

  it('is false for the same spelling, a different address, nothing typed or no current', () => {
    expect(onlyLetterCaseChanged('[email protected]', '[email protected]')).toBe(false)
    expect(onlyLetterCaseChanged('[email protected]', '[email protected]')).toBe(false)
    expect(onlyLetterCaseChanged('', '[email protected]')).toBe(false)
    expect(onlyLetterCaseChanged('[email protected]', undefined)).toBe(false)
  })

  it('folds ASCII only, as NOCASE does, so a non-ASCII case difference is a real change', () => {
    expect(onlyLetterCaseChanged('MÜ[email protected]', 'mü[email protected]')).toBe(false)
  })
})

describe('emailChanged', () => {
  it('is false for nothing typed or the same address', () => {
    expect(emailChanged('', '[email protected]')).toBe(false)
    expect(emailChanged('   ', '[email protected]')).toBe(false)
    expect(emailChanged('[email protected]', '[email protected]')).toBe(false)
    expect(emailChanged(' [email protected] ', '[email protected]')).toBe(false)
  })

  it('counts a letter-case change, which the server allows', () => {
    expect(emailChanged('[email protected]', '[email protected]')).toBe(true)
  })

  it('is true for a different address, or when there is no current one', () => {
    expect(emailChanged('[email protected]', '[email protected]')).toBe(true)
    expect(emailChanged('[email protected]', undefined)).toBe(true)
  })
})