Skip to content

Commit 8f53800

Browse files
Merge pull request #331 from ParthSinghPS/add-truncateText-function
Add the truncateText utility function with unit tests using jest
2 parents a9061e7 + dc0f3e6 commit 8f53800

File tree

5 files changed

+7142
-2568
lines changed

5 files changed

+7142
-2568
lines changed

frontend/jest.config.ts

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/**
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
import type {Config} from 'jest';
7+
8+
const config: Config = {
9+
// All imported modules in your tests should be mocked automatically
10+
// automock: false,
11+
12+
// Stop running tests after `n` failures
13+
// bail: 0,
14+
15+
// The directory where Jest should store its cached dependency information
16+
// cacheDirectory: "C:\\Users\\Parth Singh\\AppData\\Local\\Temp\\jest",
17+
18+
// Automatically clear mock calls, instances, contexts and results before every test
19+
clearMocks: true,
20+
21+
// Indicates whether the coverage information should be collected while executing the test
22+
// collectCoverage: false,
23+
24+
// An array of glob patterns indicating a set of files for which coverage information should be collected
25+
// collectCoverageFrom: undefined,
26+
27+
// The directory where Jest should output its coverage files
28+
// coverageDirectory: undefined,
29+
30+
// An array of regexp pattern strings used to skip coverage collection
31+
// coveragePathIgnorePatterns: [
32+
// "\\\\node_modules\\\\"
33+
// ],
34+
35+
// Indicates which provider should be used to instrument code for coverage
36+
coverageProvider: "v8",
37+
38+
// A list of reporter names that Jest uses when writing coverage reports
39+
// coverageReporters: [
40+
// "json",
41+
// "text",
42+
// "lcov",
43+
// "clover"
44+
// ],
45+
46+
// An object that configures minimum threshold enforcement for coverage results
47+
// coverageThreshold: undefined,
48+
49+
// A path to a custom dependency extractor
50+
// dependencyExtractor: undefined,
51+
52+
// Make calling deprecated APIs throw helpful error messages
53+
// errorOnDeprecated: false,
54+
55+
// The default configuration for fake timers
56+
// fakeTimers: {
57+
// "enableGlobally": false
58+
// },
59+
60+
// Force coverage collection from ignored files using an array of glob patterns
61+
// forceCoverageMatch: [],
62+
63+
// A path to a module which exports an async function that is triggered once before all test suites
64+
// globalSetup: undefined,
65+
66+
// A path to a module which exports an async function that is triggered once after all test suites
67+
// globalTeardown: undefined,
68+
69+
// A set of global variables that need to be available in all test environments
70+
// globals: {},
71+
72+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
73+
// maxWorkers: "50%",
74+
75+
// An array of directory names to be searched recursively up from the requiring module's location
76+
// moduleDirectories: [
77+
// "node_modules"
78+
// ],
79+
80+
// An array of file extensions your modules use
81+
// moduleFileExtensions: [
82+
// "js",
83+
// "mjs",
84+
// "cjs",
85+
// "jsx",
86+
// "ts",
87+
// "mts",
88+
// "cts",
89+
// "tsx",
90+
// "json",
91+
// "node"
92+
// ],
93+
94+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
95+
// moduleNameMapper: {},
96+
97+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
98+
// modulePathIgnorePatterns: [],
99+
100+
// Activates notifications for test results
101+
// notify: false,
102+
103+
// An enum that specifies notification mode. Requires { notify: true }
104+
// notifyMode: "failure-change",
105+
106+
// A preset that is used as a base for Jest's configuration
107+
// preset: undefined,
108+
109+
// Run tests from one or more projects
110+
// projects: undefined,
111+
112+
// Use this configuration option to add custom reporters to Jest
113+
// reporters: undefined,
114+
115+
// Automatically reset mock state before every test
116+
// resetMocks: false,
117+
118+
// Reset the module registry before running each individual test
119+
// resetModules: false,
120+
121+
// A path to a custom resolver
122+
// resolver: undefined,
123+
124+
// Automatically restore mock state and implementation before every test
125+
// restoreMocks: false,
126+
127+
// The root directory that Jest should scan for tests and modules within
128+
// rootDir: undefined,
129+
130+
// A list of paths to directories that Jest should use to search for files in
131+
// roots: [
132+
// "<rootDir>"
133+
// ],
134+
135+
// Allows you to use a custom runner instead of Jest's default test runner
136+
// runner: "jest-runner",
137+
138+
// The paths to modules that run some code to configure or set up the testing environment before each test
139+
// setupFiles: [],
140+
141+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
142+
// setupFilesAfterEnv: [],
143+
144+
// The number of seconds after which a test is considered as slow and reported as such in the results.
145+
// slowTestThreshold: 5,
146+
147+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
148+
// snapshotSerializers: [],
149+
150+
// The test environment that will be used for testing
151+
testEnvironment: "jsdom",
152+
153+
// Options that will be passed to the testEnvironment
154+
// testEnvironmentOptions: {},
155+
156+
// Adds a location field to test results
157+
// testLocationInResults: false,
158+
159+
// The glob patterns Jest uses to detect test files
160+
// testMatch: [
161+
// "**/__tests__/**/*.?([mc])[jt]s?(x)",
162+
// "**/?(*.)+(spec|test).?([mc])[jt]s?(x)"
163+
// ],
164+
165+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
166+
// testPathIgnorePatterns: [
167+
// "\\\\node_modules\\\\"
168+
// ],
169+
170+
// The regexp pattern or array of patterns that Jest uses to detect test files
171+
// testRegex: [],
172+
173+
// This option allows the use of a custom results processor
174+
// testResultsProcessor: undefined,
175+
176+
// This option allows use of a custom test runner
177+
// testRunner: "jest-circus/runner",
178+
179+
// A map from regular expressions to paths to transformers
180+
// transform: undefined,
181+
182+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
183+
// transformIgnorePatterns: [
184+
// "\\\\node_modules\\\\",
185+
// "\\.pnp\\.[^\\\\]+$"
186+
// ],
187+
188+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
189+
// unmockedModulePathPatterns: undefined,
190+
191+
// Indicates whether each individual test should be reported during the run
192+
// verbose: undefined,
193+
194+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
195+
// watchPathIgnorePatterns: [],
196+
197+
// Whether to use watchman for file crawling
198+
// watchman: true,
199+
preset: "ts-jest",
200+
transform: {
201+
"^.+\\.(ts|tsx)$": "ts-jest", // transform TypeScript
202+
},
203+
moduleFileExtensions: ["ts", "tsx", "js", "jsx"],
204+
testMatch: ["**/__tests__/**/*.test.(ts|tsx)", "**/?(*.)+(spec|test).[tj]s?(x)"],
205+
};
206+
207+
export default config;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { truncateText } from "../utils/truncateText";
2+
3+
describe("truncateText", () => {
4+
it("should return full text if within limit", () => {
5+
expect(truncateText("Hi", 5)).toBe("Hi");
6+
expect(truncateText("Hello", 5)).toBe("Hello");
7+
});
8+
9+
it("should truncate text and add ellipsis if longer than limit", () => {
10+
expect(truncateText("Hello World", 5)).toBe("Hello...");
11+
expect(truncateText("Blockchain", 4)).toBe("Bloc...");
12+
});
13+
14+
it("should handle empty strings", () => {
15+
expect(truncateText("", 5)).toBe("");
16+
});
17+
18+
it("should handle maxLength = 0", () => {
19+
expect(truncateText("Hello", 0)).toBe("...");
20+
});
21+
});

frontend/lib/utils/truncateText.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function truncateText(text: string, maxLength: number): string {
2+
if (text.length <= maxLength) {
3+
return text;
4+
}
5+
return text.slice(0, maxLength) + "...";
6+
}

0 commit comments

Comments
 (0)