Skip to content

Commit 27b1ac8

Browse files
committed
added test
1 parent b00d94c commit 27b1ac8

File tree

4 files changed

+406
-258
lines changed

4 files changed

+406
-258
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
export const getTabIdFromWeeklyTitle = jest.fn();
1+
// src/google/sheetDataMethods/__mocks__/getSheetInfo.js
2+
// This is the manual mock for ../google/sheetDataMethods/getSheetInfo.js
3+
4+
const mockInstance = jest.fn(() => Promise.resolve([])); // Default implementation
5+
6+
// Add a property to identify this specific mock instance for debugging
7+
mockInstance.isManualMock = true;
8+
9+
export const getExistingTabTitlesInRange = mockInstance;
10+
11+
// If the original getSheetInfo.js has other named exports,
12+
// you would define and export them here as well, e.g.:
13+
// export const anotherFunction = jest.fn();
14+
// anotherFunction.isManualMock = true; // and mark them too if needed

src/index.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,42 @@ jest.mock('./index.js', () => {
6565
// Import after mocking
6666
import { main } from './index.js';
6767

68+
let mockProcessExit;
69+
6870
describe('main function', () => {
71+
beforeAll(() => {
72+
mockProcessExit = jest.spyOn(process, 'exit').mockImplementation((code) => {
73+
// console.warn(`Test Warning: process.exit(${code}) was called and suppressed.`);
74+
// Optionally throw an error here if a test should explicitly fail when exit is called:
75+
// throw new Error(`process.exit(${code}) called`);
76+
});
77+
78+
global.shadowConfigDetails = {
79+
googleSpreadsheetUrl:
80+
'https://docs.google.com/spreadsheets/d/default_mock_id/edit',
81+
googleKeyFilePath: 'default/mock/path/to/keyfile.json',
82+
testTypes: ['unit', 'integration', 'e2e'],
83+
testCategories: ['smoke', 'regression', 'performance'],
84+
weeklySummaryStartDay: 'Monday',
85+
weeklySummaryEnabled: true,
86+
teamNames: ['team1', 'team2'],
87+
csvDownloadsPath: './cypress/downloads',
88+
// Ensure all fields that might be accessed by constants.js are here
89+
// For TEST_DATA, it might need specific fields like testDataPath or similar
90+
testDataPath: 'mock/path/to/test-data.json', // Assuming TEST_DATA needs something like this
91+
resultsPath: 'mock/path/to/results.json', // Assuming TEST_DATA needs something like this
92+
};
93+
});
94+
95+
afterAll(() => {
96+
mockProcessExit.mockRestore();
97+
delete global.shadowConfigDetails;
98+
});
99+
69100
beforeEach(() => {
70101
// Reset all mocks before each test
71102
jest.clearAllMocks();
103+
// mockProcessExit.mockClear(); // Clear calls to process.exit if needed per test
72104

73105
// Mock console methods
74106
console.log = jest.fn();

src/sharedMethods/summaryRequired.js

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,64 @@ const isSummaryNeeded = (tabTitles) => {
7777
* @param {boolean} options.csv - If true, outputs in CSV format.
7878
* @returns {Promise<boolean>} True if a summary is required, false otherwise.
7979
*/
80-
export const isSummaryRequired = async ({ csv }) => {
81-
if (csv) {
82-
return false;
83-
}
80+
export const isSummaryRequired = async (options = {}) => {
81+
if (options.csv) return false;
8482
try {
83+
const currentDate = new Date();
84+
// console.log('[DEBUG] SUT currentDate:', currentDate.toISOString());
85+
86+
const lastMonth = getFormattedMonth('lastMonth');
87+
// console.log('[DEBUG] SUT lastMonth:', lastMonth);
88+
89+
const currentMonth = getFormattedMonth();
90+
// console.log('[DEBUG] SUT currentMonth:', currentMonth);
91+
92+
let yearForSummary = getFormattedYear();
93+
if (currentMonth === 'Jan' && lastMonth === 'Dec') {
94+
yearForSummary = getFormattedYear('lastYear');
95+
}
96+
// console.log('[DEBUG] SUT yearForSummary:', yearForSummary);
97+
98+
const targetSummaryTitle = createSummaryTitle(
99+
lastMonth,
100+
yearForSummary.toString()
101+
);
102+
// console.log('[DEBUG] SUT targetSummaryTitle:', targetSummaryTitle);
103+
85104
const existingTabTitles = await getExistingTabTitlesInRange();
86-
return isSummaryNeeded(existingTabTitles);
105+
// console.log('[DEBUG] SUT existingTabTitles raw:', existingTabTitles);
106+
107+
if (!existingTabTitles) {
108+
// console.error('Error: existingTabTitles is undefined');
109+
return false;
110+
}
111+
112+
const summaryExists = existingTabTitles.some(
113+
(title) =>
114+
title.trim().toLowerCase() === targetSummaryTitle.trim().toLowerCase()
115+
);
116+
// console.log('[DEBUG] SUT summaryExists:', summaryExists);
117+
118+
if (summaryExists) {
119+
return false;
120+
}
121+
122+
const searchPatternMonth = lastMonth;
123+
const searchPatternYear = yearForSummary.toString();
124+
// console.log('[DEBUG] SUT searchPatternMonth:', searchPatternMonth);
125+
// console.log('[DEBUG] SUT searchPatternYear:', searchPatternYear);
126+
127+
const sheetsFromLastMonthExist = existingTabTitles.some((title) => {
128+
const titleIncludesMonth = title.includes(searchPatternMonth);
129+
const titleIncludesYear = title.includes(searchPatternYear);
130+
return titleIncludesMonth && titleIncludesYear;
131+
});
132+
// console.log(
133+
// '[DEBUG] SUT sheetsFromLastMonthExist:',
134+
// sheetsFromLastMonthExist
135+
// );
136+
137+
return sheetsFromLastMonthExist;
87138
} catch (error) {
88139
console.error('Error in isSummaryRequired:', error);
89140
return false;
@@ -142,12 +193,19 @@ const isWeeklySummaryNeeded = (tabTitles) => {
142193
return false;
143194
}
144195
const expectedSummaryTitle = createWeeklySummaryTitle();
145-
const summaryExists = tabTitles.some(
146-
(title) => title.startsWith('Weekly') && title === expectedSummaryTitle
147-
);
196+
// console.log('[SUT DEBUG] isWeeklySummaryNeeded - expectedSummaryTitle:', expectedSummaryTitle);
197+
// console.log('[SUT DEBUG] isWeeklySummaryNeeded - tabTitles:', JSON.stringify(tabTitles));
198+
199+
const summaryExists = tabTitles.some((title) => {
200+
const starts = title.startsWith('Weekly');
201+
const matches = title === expectedSummaryTitle;
202+
// console.log(`[SUT DEBUG] Checking title: "${title}", startsWeekly: ${starts}, matchesExpected: ${matches}`);
203+
return starts && matches;
204+
});
205+
// console.log('[SUT DEBUG] isWeeklySummaryNeeded - summaryExists:', summaryExists);
148206
return !summaryExists;
149207
} catch (error) {
150-
console.error('Error in isSummaryNeeded:', error);
208+
console.error('Error in isWeeklySummaryNeeded:', error);
151209
return false;
152210
}
153211
};

0 commit comments

Comments
 (0)