Hone logo
Hone
Problems

Testing a Workspace Management System with Jest

This challenge focuses on writing unit tests for a simple workspace management system using Jest and TypeScript. Workspaces allow users to organize and group related files and settings. Testing this system ensures the core functionality of creating, retrieving, updating, and deleting workspaces behaves as expected, contributing to a robust and reliable application.

Problem Description

You are tasked with creating unit tests for a Workspace class that manages workspaces. The Workspace class has the following methods:

  • create(name: string): void: Creates a new workspace with the given name. Throws an error if a workspace with the same name already exists.
  • get(name: string): string | null: Retrieves the name of a workspace by its name. Returns null if the workspace does not exist.
  • update(oldName: string, newName: string): void: Renames an existing workspace from oldName to newName. Throws an error if the oldName workspace does not exist.
  • delete(name: string): void: Deletes a workspace with the given name. Throws an error if the workspace does not exist.
  • getAll(): string[]: Returns an array containing the names of all workspaces.

Your goal is to write Jest tests to verify the correct behavior of these methods, including error handling for invalid inputs and edge cases. You should aim for comprehensive test coverage, ensuring all functionalities are thoroughly tested.

Examples

Example 1:

Input:
  - create("WorkspaceA")
  - create("WorkspaceB")
  - getAll()
Output: ["WorkspaceA", "WorkspaceB"]
Explanation: Two workspaces are created, and getAll() returns them in an array.

Example 2:

Input:
  - create("WorkspaceC")
  - get("WorkspaceC")
Output: "WorkspaceC"
Explanation: A workspace is created, and get() correctly retrieves its name.

Example 3:

Input:
  - create("WorkspaceD")
  - update("WorkspaceD", "WorkspaceE")
  - get("WorkspaceE")
Output: "WorkspaceE"
Explanation: A workspace is created, renamed, and get() now returns the new name.

Example 4: (Edge Case - Error Handling)

Input:
  - create("WorkspaceF")
  - create("WorkspaceF") // Attempt to create a duplicate workspace
Output: Error: Workspace with name 'WorkspaceF' already exists.
Explanation:  Attempting to create a workspace with a name that already exists should throw an error.

Constraints

  • The Workspace class is provided (see below). You are not allowed to modify the class itself.
  • All tests must be written in TypeScript using Jest.
  • Tests should cover both successful scenarios and error conditions.
  • The order of workspaces returned by getAll() is not guaranteed and should not be tested.
  • Error messages should match the expected messages (e.g., "Workspace with name '...' already exists.").

Notes

  • Consider using Jest's expect functions to assert the expected outcomes of your tests.
  • Think about how to effectively mock or stub any external dependencies (although this class has none).
  • Focus on writing clear, concise, and well-documented tests.
  • Use try...catch blocks to handle expected errors and assert that the correct error is thrown.
class Workspace {
    private workspaces: { [name: string]: string } = {};

    create(name: string): void {
        if (this.workspaces[name]) {
            throw new Error(`Workspace with name '${name}' already exists.`);
        }
        this.workspaces[name] = name;
    }

    get(name: string): string | null {
        return this.workspaces[name] || null;
    }

    update(oldName: string, newName: string): void {
        if (!this.workspaces[oldName]) {
            throw new Error(`Workspace with name '${oldName}' does not exist.`);
        }
        this.workspaces[newName] = this.workspaces[oldName];
        delete this.workspaces[oldName];
    }

    delete(name: string): void {
        if (!this.workspaces[name]) {
            throw new Error(`Workspace with name '${name}' does not exist.`);
        }
        delete this.workspaces[name];
    }

    getAll(): string[] {
        return Object.keys(this.workspaces);
    }
}
Loading editor...
typescript