widget.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Context,
  5. DocumentRegistry,
  6. TextModelFactory
  7. } from '@jupyterlab/docregistry';
  8. import * as Mock from '@jupyterlab/testutils/lib/mock';
  9. import { UUID } from '@lumino/coreutils';
  10. import { CellRenderer, DataGrid, JSONModel } from '@lumino/datagrid';
  11. import { CSVViewer, GridSearchService } from '../src';
  12. function createContext(): Context<DocumentRegistry.IModel> {
  13. const factory = new TextModelFactory();
  14. const manager = new Mock.ServiceManagerMock();
  15. const path = UUID.uuid4() + '.csv';
  16. return new Context({ factory, manager, path });
  17. }
  18. describe('csvviewer/widget', () => {
  19. const context = createContext();
  20. describe('CSVViewer', () => {
  21. describe('#constructor()', () => {
  22. it('should instantiate a `CSVViewer`', () => {
  23. const widget = new CSVViewer({ context });
  24. expect(widget).toBeInstanceOf(CSVViewer);
  25. widget.dispose();
  26. });
  27. });
  28. describe('#context', () => {
  29. it('should be the context for the file', () => {
  30. const widget = new CSVViewer({ context });
  31. expect(widget.context).toBe(context);
  32. });
  33. });
  34. describe('#dispose()', () => {
  35. it('should dispose of the resources held by the widget', () => {
  36. const widget = new CSVViewer({ context });
  37. expect(widget.isDisposed).toBe(false);
  38. widget.dispose();
  39. expect(widget.isDisposed).toBe(true);
  40. });
  41. it('should be safe to call multiple times', () => {
  42. const widget = new CSVViewer({ context });
  43. expect(widget.isDisposed).toBe(false);
  44. widget.dispose();
  45. widget.dispose();
  46. expect(widget.isDisposed).toBe(true);
  47. });
  48. });
  49. });
  50. describe('GridSearchService', () => {
  51. function createModel(): JSONModel {
  52. return new JSONModel({
  53. data: [
  54. { index: 0, a: 'other', b: 'match 1' },
  55. { index: 1, a: 'other', b: 'match 2' }
  56. ],
  57. schema: {
  58. primaryKey: ['index'],
  59. fields: [
  60. {
  61. name: 'a'
  62. },
  63. { name: 'b' }
  64. ]
  65. }
  66. });
  67. }
  68. function createGridSearchService(model: JSONModel): GridSearchService {
  69. const grid = new DataGrid();
  70. grid.dataModel = model;
  71. return new GridSearchService(grid);
  72. }
  73. it('searches incrementally and set background color', () => {
  74. const model = createModel();
  75. const searchService = createGridSearchService(model);
  76. const cellRenderer = searchService.cellBackgroundColorRendererFunc({
  77. matchBackgroundColor: 'anotherMatch',
  78. currentMatchBackgroundColor: 'currentMatch',
  79. textColor: '',
  80. horizontalAlignment: 'right'
  81. });
  82. /**
  83. * fake rendering a cell and returns the background color for this coordinate.
  84. */
  85. function fakeRenderCell(row: number, column: number) {
  86. const cellConfig = {
  87. value: model.data('body', row, column),
  88. row,
  89. column
  90. } as CellRenderer.CellConfig;
  91. return cellRenderer(cellConfig);
  92. }
  93. // searching for "match", cells at (0,1) and (1,1) should match.
  94. // (0,1) is the current match
  95. const query = /match/;
  96. searchService.find(query);
  97. expect(fakeRenderCell(0, 1)).toBe('currentMatch');
  98. expect(fakeRenderCell(1, 1)).toBe('anotherMatch');
  99. expect(fakeRenderCell(0, 0)).toBe('');
  100. // search again, the current match "moves" to be (1,1)
  101. searchService.find(query);
  102. expect(fakeRenderCell(0, 1)).toBe('anotherMatch');
  103. expect(fakeRenderCell(1, 1)).toBe('currentMatch');
  104. });
  105. });
  106. });