pageconfig.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { PageConfig } from '@jupyterlab/coreutils';
  4. describe('@jupyterlab/coreutils', () => {
  5. describe('PageConfig', () => {
  6. beforeEach(() => {
  7. PageConfig.setOption('foo', 'bar');
  8. PageConfig.setOption('workspace', PageConfig.defaultWorkspace);
  9. });
  10. describe('#getOption()', () => {
  11. it('should get a known option', () => {
  12. expect(PageConfig.getOption('foo')).toBe('bar');
  13. });
  14. it('should return an empty string for an unknown option', () => {
  15. expect(PageConfig.getOption('bar')).toBe('');
  16. });
  17. });
  18. describe('#setOption()', () => {
  19. it('should get last option value and set it to the passed value', () => {
  20. expect(PageConfig.setOption('foo', 'bar1')).toBe('bar');
  21. });
  22. it('should get a known option', () => {
  23. expect(PageConfig.getOption('foo')).toBe('bar');
  24. });
  25. it('should add a new option', () => {
  26. expect(PageConfig.setOption('bar', 'foo')).toBe('');
  27. });
  28. it('should get a different known option', () => {
  29. expect(PageConfig.getOption('bar')).toBe('foo');
  30. });
  31. });
  32. describe('#getBaseUrl()', () => {
  33. it('should get the base url of the page', () => {
  34. // The value was passed as a command line arg.
  35. expect(PageConfig.getBaseUrl()).toContain('http://localhost');
  36. });
  37. });
  38. describe('#getWsUrl()', () => {
  39. it('should get the base ws url of the page', () => {
  40. // The value was passed as a command line arg.
  41. const expected = 'ws' + PageConfig.getBaseUrl().slice(4);
  42. expect(PageConfig.getWsUrl()).toBe(expected);
  43. });
  44. it('should handle a good base url', () => {
  45. const url = 'http://foo.com';
  46. expect(PageConfig.getWsUrl(url)).toBe('ws://foo.com/');
  47. });
  48. it('should be an empty string for a bad base url', () => {
  49. const url = 'blargh://foo.com';
  50. expect(PageConfig.getWsUrl(url)).toBe('');
  51. });
  52. });
  53. describe('#getUrl()', () => {
  54. const path = '/path/to/file.ext';
  55. it('should return shortest url by default', () => {
  56. const url = PageConfig.getUrl({});
  57. expect(url).toEqual('http://localhost/lab');
  58. });
  59. it('should return a local shareable url if shareUrl is undefined', () => {
  60. const url = PageConfig.getUrl({
  61. workspace: PageConfig.defaultWorkspace,
  62. treePath: path,
  63. toShare: true
  64. });
  65. expect(url).toEqual(`http://localhost/lab/tree${path}`);
  66. });
  67. describe('hub environment', () => {
  68. const shareUrl = 'http://hub.host.lab/hub/user-redirect';
  69. beforeEach(() => {
  70. PageConfig.setOption('shareUrl', shareUrl);
  71. });
  72. it('should return a non-local shareable url if shareUrl is defined', () => {
  73. const url = PageConfig.getUrl({
  74. workspace: PageConfig.defaultWorkspace,
  75. treePath: path,
  76. toShare: true
  77. });
  78. expect(url).toEqual(`${shareUrl}/lab/tree${path}`);
  79. });
  80. });
  81. });
  82. });
  83. });