widget.spec.ts 3.6 KB

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