web-api-utils-test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Map as makeMap, OrderedMap as makeOrderedMap } from 'immutable';
  2. import {
  3. buildUrlQuery, basePath, getApiPath, getWebsocketUrl
  4. } from '../web-api-utils';
  5. describe('WebApiUtils', () => {
  6. describe('basePath', () => {
  7. it('should handle /scope/terminal.html', () => {
  8. expect(basePath('/scope/terminal.html')).toBe('/scope');
  9. });
  10. it('should handle /scope/', () => {
  11. expect(basePath('/scope/')).toBe('/scope');
  12. });
  13. it('should handle /scope', () => {
  14. expect(basePath('/scope')).toBe('/scope');
  15. });
  16. it('should handle /', () => {
  17. expect(basePath('/')).toBe('');
  18. });
  19. });
  20. describe('buildUrlQuery', () => {
  21. let state = makeMap();
  22. it('should handle empty options', () => {
  23. expect(buildUrlQuery(makeOrderedMap([]), state)).toBe('');
  24. });
  25. it('should combine multiple options', () => {
  26. expect(buildUrlQuery(makeOrderedMap([
  27. ['foo', 2],
  28. ['bar', 4]
  29. ]), state)).toBe('foo=2&bar=4');
  30. });
  31. it('should combine multiple options with a timestamp', () => {
  32. state = state.set('pausedAt', '2015-06-14T21:12:05.275Z');
  33. expect(buildUrlQuery(makeOrderedMap([
  34. ['foo', 2],
  35. ['bar', 4]
  36. ]), state)).toBe('foo=2&bar=4&timestamp=2015-06-14T21:12:05.275Z');
  37. });
  38. });
  39. describe('getApiPath', () => {
  40. afterEach(() => {
  41. delete process.env.SCOPE_API_PREFIX;
  42. });
  43. it('returns the correct url when running standalone', () => {
  44. expect(getApiPath('/')).toEqual('');
  45. });
  46. it('returns the correct url when running in an iframe', () => {
  47. expect(getApiPath('/api/app/proud-cloud-77')).toEqual('/api/app/proud-cloud-77');
  48. });
  49. it('returns the correct url when running as a component', () => {
  50. process.env.SCOPE_API_PREFIX = '/api';
  51. // instance ID first to match Weave Cloud routes
  52. expect(getApiPath('/proud-cloud-77/app')).toEqual('/api/app/proud-cloud-77');
  53. });
  54. it('returns the correct url from an arbitrary path', () => {
  55. expect(getApiPath('/demo/')).toEqual('/demo');
  56. });
  57. it('returns the correct url from an *.html page', () => {
  58. expect(getApiPath('/contrast.html')).toEqual('');
  59. });
  60. it('returns the correct url from an /*.html page while in an iframe', () => {
  61. expect(getApiPath('/api/app/proud-cloud-77/contrast.html')).toEqual('/api/app/proud-cloud-77');
  62. });
  63. });
  64. describe('getWebsocketUrl', () => {
  65. const host = 'localhost:4042';
  66. afterEach(() => {
  67. delete process.env.SCOPE_API_PREFIX;
  68. });
  69. it('returns the correct url when running standalone', () => {
  70. expect(getWebsocketUrl(host, '/')).toEqual(`ws://${host}`);
  71. });
  72. it('returns the correct url when running in an iframe', () => {
  73. expect(getWebsocketUrl(host, '/api/app/proud-cloud-77')).toEqual(`ws://${host}/api/app/proud-cloud-77`);
  74. });
  75. it('returns the correct url when running as a component', () => {
  76. process.env.SCOPE_API_PREFIX = '/api';
  77. expect(getWebsocketUrl(host, '/proud-cloud-77/app')).toEqual(`ws://${host}/api/app/proud-cloud-77`);
  78. });
  79. it('returns the correct url from an arbitrary path', () => {
  80. expect(getWebsocketUrl(host, '/demo/')).toEqual(`ws://${host}/demo`);
  81. });
  82. it('returns the correct url from an *.html page', () => {
  83. expect(getWebsocketUrl(host, '/contrast.html')).toEqual(`ws://${host}`);
  84. });
  85. it('returns the correct url from an /*.html page while in an iframe', () => {
  86. expect(getWebsocketUrl(host, '/api/app/proud-cloud-77/contrast.html')).toEqual(`ws://${host}/api/app/proud-cloud-77`);
  87. });
  88. });
  89. });