mirror of
https://github.com/mryanb/obsidian-asana.git
synced 2026-07-22 05:38:24 +00:00
- Add Jest configuration and TypeScript support - Create tests for main plugin functionality (initialization, settings) - Add tests for Asana API integration (workspaces, projects, tasks) - Set up mock files for Obsidian API and test environment - Add test scripts to package.json This commit sets up a basic test infrastructure to ensure the plugin's core functionality works as expected and to catch potential regressions in future changes.
155 lines
No EOL
4.7 KiB
JavaScript
155 lines
No EOL
4.7 KiB
JavaScript
const { requestUrl } = require('obsidian');
|
|
const {
|
|
fetchAsanaWorkspaces,
|
|
fetchAsanaProjects,
|
|
fetchAsanaSections,
|
|
createTaskInAsana
|
|
} = require('../src/api/asanaApi');
|
|
|
|
jest.mock('obsidian', () => ({
|
|
requestUrl: jest.fn()
|
|
}));
|
|
|
|
describe('Asana API', () => {
|
|
const mockSettings = {
|
|
asanaToken: 'test-token',
|
|
showArchivedProjects: false
|
|
};
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
test('fetchAsanaWorkspaces handles successful response', async () => {
|
|
const mockWorkspaces = [{ id: '1', name: 'Workspace 1' }];
|
|
requestUrl.mockResolvedValueOnce({
|
|
status: 200,
|
|
json: { data: mockWorkspaces }
|
|
});
|
|
|
|
const result = await fetchAsanaWorkspaces(mockSettings);
|
|
expect(result).toEqual(mockWorkspaces);
|
|
expect(requestUrl).toHaveBeenCalledWith({
|
|
url: 'https://app.asana.com/api/1.0/workspaces',
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: 'Bearer test-token'
|
|
}
|
|
});
|
|
});
|
|
|
|
test('fetchAsanaWorkspaces handles error response', async () => {
|
|
requestUrl.mockRejectedValueOnce(new Error('API Error'));
|
|
|
|
await expect(fetchAsanaWorkspaces(mockSettings)).rejects.toThrow('Failed to retrieve workspaces from Asana');
|
|
});
|
|
|
|
test('fetchAsanaProjects handles successful response', async () => {
|
|
const mockProjects = [{ id: '1', name: 'Project 1' }];
|
|
requestUrl.mockResolvedValueOnce({
|
|
status: 200,
|
|
json: { data: mockProjects }
|
|
});
|
|
|
|
const result = await fetchAsanaProjects('workspace-1', mockSettings);
|
|
expect(result).toEqual(mockProjects);
|
|
expect(requestUrl).toHaveBeenCalledWith({
|
|
url: 'https://app.asana.com/api/1.0/workspaces/workspace-1/projects?is_archived=false',
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: 'Bearer test-token'
|
|
}
|
|
});
|
|
});
|
|
|
|
test('createTaskInAsana handles successful response', async () => {
|
|
// Mock initial task creation
|
|
const mockTaskGid = '1234';
|
|
requestUrl.mockResolvedValueOnce({
|
|
status: 201,
|
|
json: { data: { gid: mockTaskGid, name: 'Test Task' } }
|
|
});
|
|
|
|
// Mock section addition
|
|
requestUrl.mockResolvedValueOnce({
|
|
status: 200,
|
|
json: { data: { gid: mockTaskGid } }
|
|
});
|
|
|
|
// Mock task details fetch
|
|
const mockTaskDetails = {
|
|
gid: mockTaskGid,
|
|
name: 'Test Task',
|
|
permalink_url: 'https://app.asana.com/0/1/1'
|
|
};
|
|
requestUrl.mockResolvedValueOnce({
|
|
status: 200,
|
|
json: { data: mockTaskDetails }
|
|
});
|
|
|
|
const result = await createTaskInAsana(
|
|
'Test Task',
|
|
'workspace-1',
|
|
'project-1',
|
|
'section-1',
|
|
mockSettings
|
|
);
|
|
|
|
expect(result).toEqual(mockTaskDetails);
|
|
expect(requestUrl).toHaveBeenCalledTimes(3);
|
|
|
|
// Verify task creation call
|
|
expect(requestUrl).toHaveBeenNthCalledWith(1, {
|
|
url: 'https://app.asana.com/api/1.0/tasks',
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-token',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
data: {
|
|
name: 'Test Task',
|
|
workspace: 'workspace-1',
|
|
projects: ['project-1'],
|
|
assignee: undefined
|
|
}
|
|
})
|
|
});
|
|
|
|
// Verify section addition call
|
|
expect(requestUrl).toHaveBeenNthCalledWith(2, {
|
|
url: 'https://app.asana.com/api/1.0/sections/section-1/addTask',
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-token',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
data: {
|
|
task: mockTaskGid
|
|
}
|
|
})
|
|
});
|
|
|
|
// Verify task details fetch call
|
|
expect(requestUrl).toHaveBeenNthCalledWith(3, {
|
|
url: `https://app.asana.com/api/1.0/tasks/${mockTaskGid}`,
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: 'Bearer test-token'
|
|
}
|
|
});
|
|
});
|
|
|
|
test('createTaskInAsana handles error response', async () => {
|
|
requestUrl.mockRejectedValueOnce(new Error('API Error'));
|
|
|
|
await expect(createTaskInAsana(
|
|
'Test Task',
|
|
'workspace-1',
|
|
'project-1',
|
|
'section-1',
|
|
mockSettings
|
|
)).rejects.toThrow('Failed to create task in Asana');
|
|
});
|
|
});
|