Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google test #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"typescript": "^5.5.4"
},
"devDependencies": {
"jest": "^29.7.0"
"jest": "^29.7.0",
"lodash": "^4.17.21"
}
}
8 changes: 8 additions & 0 deletions tests/examples/googleAnalyticsArrowFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const trackEvent = () => {
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXX/XXXXXXXXXX',
'value': 1.0,
'currency': 'USD'
});
};
trackEvent();
8 changes: 8 additions & 0 deletions tests/examples/googleAnalyticsFunctionExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const trackEvent = function() {
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXX/XXXXXXXXXX',
'value': 1.0,
'currency': 'USD'
});
};
trackEvent();
5 changes: 5 additions & 0 deletions tests/examples/googleAnalyticsGlobal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXX/XXXXXXXXXX',
'value': 1.0,
'currency': 'USD'
});
8 changes: 8 additions & 0 deletions tests/examples/googleAnalyticsRegularFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function trackEvent() {
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXX/XXXXXXXXXX',
'value': 1.0,
'currency': 'USD'
});
};
trackEvent();
6 changes: 6 additions & 0 deletions tests/expected/googleAnalyticsArrowFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
send_to:
type: string
value:
type: number
currency:
type: string
6 changes: 6 additions & 0 deletions tests/expected/googleAnalyticsFunctionExpression.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
send_to:
type: string
value:
type: number
currency:
type: string
6 changes: 6 additions & 0 deletions tests/expected/googleAnalyticsGlobal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
send_to:
type: string
value:
type: number
currency:
type: string
6 changes: 6 additions & 0 deletions tests/expected/googleAnalyticsRegularFunction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
send_to:
type: string
value:
type: number
currency:
type: string
51 changes: 51 additions & 0 deletions tests/googleAnalyticsJS.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const yaml = require('js-yaml');
const { run } = require('../src/index.js');

function extractRelevantEventData(actualYamlContent, eventName) {
const data = yaml.load(actualYamlContent);
if (data && data.events && data.events[eventName]) {
return yaml.dump(data.events[eventName].properties);
}
return null;
}

const testCases = [
{ name: 'Regular Function', file: 'googleAnalyticsRegularFunction.js', expectedFile: 'googleAnalyticsRegularFunction.yaml' },
{ name: 'Arrow Function', file: 'googleAnalyticsArrowFunction.js' , expectedFile: 'googleAnalyticsArrowFunction.yaml' },
{ name: 'Function Expression', file: 'googleAnalyticsFunctionExpression.js' , expectedFile: 'googleAnalyticsFunctionExpression.yaml' },
{ name: 'Global Context', file: 'googleAnalyticsGlobal.js' , expectedFile: 'googleAnalyticsGlobal.yaml' },
];

const outputPath = './tests/output/allEvents.yaml';

describe('Google Analytics Tests', () => {
it('should process all events correctly', async () => {
const targetDir = './tests/examples/';

await run(path.resolve(targetDir), outputPath);

testCases.forEach(testCase => {
const expectedPath = `./tests/expected/${testCase.expectedFile}`;
const expected = fs.readFileSync(expectedPath, 'utf8');
const fullOutput = fs.readFileSync(outputPath, 'utf8');
const actual = extractRelevantEventData(fullOutput, 'conversion');

if (!_.isEqual(actual, expected)) {
console.log(`Mismatch in ${testCase.name}`);
console.log("Expected:", expected);
console.log("Actual:", actual);
}

expect(_.isEqual(actual, expected)).toBe(true);
});
});

afterAll(() => {
if (fs.existsSync(outputPath)) {
fs.unlinkSync(outputPath);
}
});
});