display-name-for-user.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { expect } from 'chai'
  2. import displayNameForUser from '@/features/history/utils/display-name-for-user'
  3. describe('displayNameForUser', function () {
  4. const currentUsersId = 'user-a'
  5. beforeEach(function () {
  6. window.metaAttributesCache.set('ol-user', { id: currentUsersId })
  7. })
  8. it("should return 'Anonymous' with no user", function () {
  9. return expect(displayNameForUser(null)).to.equal('Anonymous')
  10. })
  11. it("should return 'you' when the user has the same id as the window", function () {
  12. return expect(
  13. displayNameForUser({
  14. id: currentUsersId,
  15. email: 'james.allen@overleaf.com',
  16. first_name: 'James',
  17. last_name: 'Allen',
  18. })
  19. ).to.equal('you')
  20. })
  21. it('should return the first_name and last_name when present', function () {
  22. return expect(
  23. displayNameForUser({
  24. id: currentUsersId + 1,
  25. email: 'james.allen@overleaf.com',
  26. first_name: 'James',
  27. last_name: 'Allen',
  28. })
  29. ).to.equal('James Allen')
  30. })
  31. it('should return only the first_name if no last_name', function () {
  32. return expect(
  33. displayNameForUser({
  34. id: currentUsersId + 1,
  35. email: 'james.allen@overleaf.com',
  36. first_name: 'James',
  37. last_name: '',
  38. })
  39. ).to.equal('James')
  40. })
  41. it('should return the email username if there are no names', function () {
  42. return expect(
  43. displayNameForUser({
  44. id: currentUsersId + 1,
  45. email: 'james.allen@overleaf.com',
  46. first_name: '',
  47. last_name: '',
  48. })
  49. ).to.equal('james.allen')
  50. })
  51. it("should return the '?' if it has nothing", function () {
  52. return expect(
  53. displayNameForUser({
  54. id: currentUsersId + 1,
  55. email: '',
  56. first_name: '',
  57. last_name: '',
  58. })
  59. ).to.equal('?')
  60. })
  61. })