|
| 1 | +# Testing UI Components in UCC Framework |
| 2 | + |
| 3 | +This guide shows you how to test custom UI components in your Splunk Technology Add-on (TA) using the Unified Configuration Console (UCC) framework. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## What You'll Need |
| 8 | + |
| 9 | +Before starting, make sure you have: |
| 10 | + |
| 11 | +- **Node.js** version 22 or higher |
| 12 | +- **npm** version 10 or higher |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Getting Started |
| 17 | + |
| 18 | +### 1. Set Up Your UI Project |
| 19 | + |
| 20 | +First, create your UI sub-project by following the [UI Sub-Project Setup Guide](./custom_project_init.md). |
| 21 | + |
| 22 | +### 2. Create Your Test Files |
| 23 | + |
| 24 | +Create test files anywhere in your `src` directory. Use any of these file extensions: |
| 25 | + |
| 26 | +- `.test.js`, `.test.ts`, `.test.jsx`, `.test.tsx` |
| 27 | +- `.spec.js`, `.spec.ts`, `.spec.jsx`, `.spec.tsx` |
| 28 | + |
| 29 | +The testing framework will automatically find files matching the pattern `src/**/*.{js,jsx,ts,tsx}`. |
| 30 | + |
| 31 | +### 3. Add the Test Command |
| 32 | + |
| 33 | +Update your `package.json` file to include the test command: |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "scripts": { |
| 38 | + "ucc-test": "test-ucc-ui" |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +#### Complete package.json Example |
| 44 | + |
| 45 | +If you followed the UI Sub-Project Setup Guide, your `package.json` should look like this: |
| 46 | + |
| 47 | +<details> |
| 48 | + <summary>View complete package.json</summary> |
| 49 | + |
| 50 | +```json |
| 51 | +{ |
| 52 | + "name": "ui", |
| 53 | + "private": true, |
| 54 | + "version": "0.0.0", |
| 55 | + "type": "module", |
| 56 | + "scripts": { |
| 57 | + "ucc-gen": "ucc-gen-ui ta_name=Splunk_TA_Name init_file_dir=src/ucc-ui.ts", |
| 58 | + "ucc-test": "test-ucc-ui" |
| 59 | + }, |
| 60 | + "dependencies": { |
| 61 | + "@splunk/add-on-ucc-framework": "^5.65.0", |
| 62 | + "@splunk/react-ui": "^4.42.0", |
| 63 | + "@splunk/splunk-utils": "^3.1.0", |
| 64 | + "@splunk/themes": "^0.23.0", |
| 65 | + "react": "16.14.0", |
| 66 | + "react-dom": "16.14.0" |
| 67 | + }, |
| 68 | + "devDependencies": { |
| 69 | + "@eslint/js": "^9.20.0", |
| 70 | + "@types/node": "^22.13.1", |
| 71 | + "@types/react": "16.14.62", |
| 72 | + "@types/react-dom": "16.9.25", |
| 73 | + "typescript": "^5.8.2" |
| 74 | + }, |
| 75 | + "overrides": { |
| 76 | + "react": "16.14.0", |
| 77 | + "react-dom": "16.14.0", |
| 78 | + "@types/react": "16.14.62", |
| 79 | + "@types/react-dom": "16.9.25" |
| 80 | + }, |
| 81 | + "engines": { |
| 82 | + "node": ">=22", |
| 83 | + "npm": ">=10" |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +</details> |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Testing Your UI Components |
| 93 | + |
| 94 | +The UCC framework provides two main functions to help you test your pages: |
| 95 | + |
| 96 | +### Testing Configuration Pages |
| 97 | + |
| 98 | +Use `renderConfigurationPage()` to test your configuration pages (like account settings). |
| 99 | + |
| 100 | +**Parameters:** |
| 101 | + |
| 102 | +- `globalConfig` (required): Your globalConfig.json file content |
| 103 | +- `customComponents` (optional): Object containing your custom UI components |
| 104 | + |
| 105 | +**Example:** |
| 106 | + |
| 107 | +```typescript |
| 108 | +import { screen, waitForElementToBeRemoved } from "@testing-library/react"; |
| 109 | +import { it, expect } from "vitest"; |
| 110 | +import userEvent from "@testing-library/user-event"; |
| 111 | + |
| 112 | +import { getGlobalConfig } from "./utils"; |
| 113 | +import AdvancedInputsTabClass from "../ucc-ui-extensions/AdvancedInputsTab"; |
| 114 | +import DateInputClass from "../ucc-ui-extensions/DateInput"; |
| 115 | + |
| 116 | +it("Should open account addition form", async () => { |
| 117 | + mockResponse(); |
| 118 | + |
| 119 | + renderConfigurationPage(getGlobalConfig(), { |
| 120 | + DateInput: { |
| 121 | + component: DateInputClass, |
| 122 | + type: "control", |
| 123 | + }, |
| 124 | + AdvancedInputsTab: { |
| 125 | + component: AdvancedInputsTabClass, |
| 126 | + type: "tab", |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + // Wait for page to load |
| 131 | + await waitForElementToBeRemoved(() => screen.getByText("Waiting")); |
| 132 | + |
| 133 | + // Check if page elements are present |
| 134 | + expect(screen.getByText("Configuration")).toBeInTheDocument(); |
| 135 | + expect(await screen.findByText("Mocked Account name")).toBeInTheDocument(); |
| 136 | + |
| 137 | + // Test clicking the Add button |
| 138 | + const addButton = screen.getByRole("button", { name: "Add" }); |
| 139 | + expect(addButton).toBeInTheDocument(); |
| 140 | + |
| 141 | + await userEvent.click(addButton); |
| 142 | + expect(await screen.findByText("Add Accounts")).toBeInTheDocument(); |
| 143 | +}); |
| 144 | +``` |
| 145 | + |
| 146 | +### Testing Input Pages |
| 147 | + |
| 148 | +Use `renderInputsPage()` to test your input pages (like data input configurations). |
| 149 | + |
| 150 | +**Parameters:** Same as `renderConfigurationPage()` |
| 151 | + |
| 152 | +- `globalConfig` (required): Your globalConfig.json file content |
| 153 | +- `customComponents` (optional): Object containing your custom UI components |
| 154 | + |
| 155 | +**Example:** |
| 156 | + |
| 157 | +```typescript |
| 158 | +import { screen, waitForElementToBeRemoved } from "@testing-library/react"; |
| 159 | +import { it, expect } from "vitest"; |
| 160 | +import userEvent from "@testing-library/user-event"; |
| 161 | + |
| 162 | +import { getGlobalConfig } from "./utils"; |
| 163 | +import AdvancedInputsTabClass from "../ucc-ui-extensions/AdvancedInputsTab"; |
| 164 | +import DateInputClass from "../ucc-ui-extensions/DateInput"; |
| 165 | + |
| 166 | +it("Should open inputs addition form", async () => { |
| 167 | + mockResponse(); |
| 168 | + |
| 169 | + renderInputsPage(getGlobalConfig(), { |
| 170 | + DateInput: { |
| 171 | + component: DateInputClass, |
| 172 | + type: "control", |
| 173 | + }, |
| 174 | + AdvancedInputsTab: { |
| 175 | + component: AdvancedInputsTabClass, |
| 176 | + type: "tab", |
| 177 | + }, |
| 178 | + }); |
| 179 | + |
| 180 | + // Wait for page to load |
| 181 | + await waitForElementToBeRemoved(() => screen.getByText("Waiting")); |
| 182 | + |
| 183 | + // Check if page elements are present |
| 184 | + expect(screen.getByText("Inputs")).toBeInTheDocument(); |
| 185 | + expect(await screen.findByText("Mocked Input name")).toBeInTheDocument(); |
| 186 | + |
| 187 | + // Test clicking the Create button |
| 188 | + const createButton = screen.getByRole("button", { name: "Create New Input" }); |
| 189 | + expect(createButton).toBeInTheDocument(); |
| 190 | + |
| 191 | + await userEvent.click(createButton); |
| 192 | + expect(await screen.findByText("Add Example service name")).toBeInTheDocument(); |
| 193 | +}); |
| 194 | +``` |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## Testing Best Practices |
| 199 | + |
| 200 | +### Mock API Responses |
| 201 | + |
| 202 | +We recommend using [MSW (Mock Service Worker)](https://mswjs.io/) to mock API calls in your tests. |
| 203 | + |
| 204 | +**1. Set up the server (server.ts):** |
| 205 | + |
| 206 | +```typescript |
| 207 | +import { setupServer } from "msw/node"; |
| 208 | +import { afterAll, afterEach } from "vitest"; |
| 209 | + |
| 210 | +export const server = setupServer(); |
| 211 | + |
| 212 | +server.listen({ |
| 213 | + onUnhandledRequest: "warn", |
| 214 | +}); |
| 215 | + |
| 216 | +afterEach(() => server.resetHandlers()); |
| 217 | +afterAll(() => server.close()); |
| 218 | + |
| 219 | +process.once("SIGINT", () => server.close()); |
| 220 | +process.once("SIGTERM", () => server.close()); |
| 221 | +``` |
| 222 | + |
| 223 | +**2. Mock responses in your tests:** |
| 224 | + |
| 225 | +```typescript |
| 226 | +function mockResponse() { |
| 227 | + server.use( |
| 228 | + http.get(`/servicesNS/nobody/-/:endpointUrl/:serviceName`, () => { |
| 229 | + return HttpResponse.json(mockServerResponseWithContent); |
| 230 | + }), |
| 231 | + http.get(`/servicesNS/nobody/-/:endpointUrl`, () => { |
| 232 | + return HttpResponse.json(mockServerResponseWithContent); |
| 233 | + }) |
| 234 | + ); |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +**3. Use standard response format:** |
| 239 | + |
| 240 | +```typescript |
| 241 | +export const mockServerResponseWithContent = { |
| 242 | + links: { |
| 243 | + create: `/servicesNS/nobody/Splunk_TA_Example/account/_new`, |
| 244 | + }, |
| 245 | + updated: "2023-08-21T11:54:12+00:00", |
| 246 | + entry: [ |
| 247 | + { |
| 248 | + id: 1, |
| 249 | + name: "Mocked Input name", |
| 250 | + content: { |
| 251 | + disabled: true, |
| 252 | + fields1: "value1", |
| 253 | + fields2: "value2", |
| 254 | + }, |
| 255 | + }, |
| 256 | + ], |
| 257 | + messages: [], |
| 258 | +}; |
| 259 | +``` |
| 260 | + |
| 261 | +--- |
| 262 | + |
| 263 | +## Running Your Tests |
| 264 | + |
| 265 | +Once everything is set up, run your tests with: |
| 266 | + |
| 267 | +```bash |
| 268 | +npm run ucc-test |
| 269 | +``` |
| 270 | + |
| 271 | +This will execute all test files in your `src` directory and show you the results. |
0 commit comments