@@ -2,12 +2,14 @@ import React from "react";
22import { render , screen , waitFor } from "@testing-library/react" ;
33import { TextViewer } from "../Result/text_viewer" ;
44import fetchMock from "jest-fetch-mock" ;
5+ import userEvent from "@testing-library/user-event" ;
56import { BinaryViewer } from "../Result/binary_viewer" ;
67
78describe ( "TextViewer" , ( ) => {
89 const successfulFetchResp = "File content" ;
910 const loadingCallback = jest . fn ( ) ;
1011 const errorCallback = jest . fn ( ) ;
12+ const getItemMock = jest . spyOn ( Storage . prototype , "getItem" ) ;
1113 const props = {
1214 annotations : [ ] ,
1315 focusLine : null ,
@@ -21,6 +23,38 @@ describe("TextViewer", () => {
2123 fetchMock . resetMocks ( ) ;
2224 } ) ;
2325
26+ it ( "should save font size to localStorage when font size change" , async ( ) => {
27+ jest . spyOn ( Storage . prototype , "setItem" ) ;
28+
29+ render ( < TextViewer { ...props } /> ) ;
30+ userEvent . click ( screen . getByText ( "+A" ) ) ;
31+
32+ await waitFor ( ( ) => {
33+ expect ( localStorage . setItem ) . toHaveBeenCalledWith ( "text_viewer_font_size" , 1.25 ) ;
34+ } ) ;
35+ } ) ;
36+
37+ it ( "should remove local storage text_viewer_font_size when it is not a number" , async ( ) => {
38+ localStorage . setItem ( "text_viewer_font_size" , "not a number" ) ;
39+
40+ render ( < TextViewer { ...props } /> ) ;
41+
42+ await waitFor ( ( ) => {
43+ expect ( localStorage . getItem ( "text_viewer_font_size" ) ) . toBeNull ( ) ;
44+ } ) ;
45+ } ) ;
46+
47+ it ( "should render using font size from localStorage" , async ( ) => {
48+ getItemMock . mockReturnValue ( "3" ) ;
49+
50+ const { container} = render ( < TextViewer { ...props } /> ) ;
51+ const element = container . querySelector ( ".line-numbers" ) ;
52+
53+ await waitFor ( ( ) => {
54+ expect ( element ) . toHaveStyle ( "font-size: 3em" ) ;
55+ } ) ;
56+ } ) ;
57+
2458 it ( "should render its text content when the content ends with a new line" , ( ) => {
2559 render ( < TextViewer { ...props } content = { "def f(n: int) -> int:\n return n + 1\n" } /> ) ;
2660
0 commit comments