From 648f1e139278cb34376ced46cb1f380f7a78f436 Mon Sep 17 00:00:00 2001 From: Natanya Anderson Date: Sat, 21 Sep 2024 20:53:29 -0400 Subject: [PATCH] google test --- package-lock.json | 10 +++- package.json | 3 +- .../examples/googleAnalyticsArrowFunction.js | 8 +++ .../googleAnalyticsFunctionExpression.js | 8 +++ tests/examples/googleAnalyticsGlobal.js | 5 ++ .../googleAnalyticsRegularFunction.js | 8 +++ .../googleAnalyticsArrowFunction.yaml | 6 +++ .../googleAnalyticsFunctionExpression.yaml | 6 +++ tests/expected/googleAnalyticsGlobal.yaml | 6 +++ .../googleAnalyticsRegularFunction.yaml | 6 +++ tests/googleAnalyticsJS.test.js | 51 +++++++++++++++++++ 11 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 tests/examples/googleAnalyticsArrowFunction.js create mode 100644 tests/examples/googleAnalyticsFunctionExpression.js create mode 100644 tests/examples/googleAnalyticsGlobal.js create mode 100644 tests/examples/googleAnalyticsRegularFunction.js create mode 100644 tests/expected/googleAnalyticsArrowFunction.yaml create mode 100644 tests/expected/googleAnalyticsFunctionExpression.yaml create mode 100644 tests/expected/googleAnalyticsGlobal.yaml create mode 100644 tests/expected/googleAnalyticsRegularFunction.yaml create mode 100644 tests/googleAnalyticsJS.test.js diff --git a/package-lock.json b/package-lock.json index 61bc973..d31f321 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,8 @@ "analyze-tracking": "bin/cli.js" }, "devDependencies": { - "jest": "^29.7.0" + "jest": "^29.7.0", + "lodash": "^4.17.21" } }, "node_modules/@ampproject/remapping": { @@ -3911,6 +3912,13 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true, + "license": "MIT" + }, "node_modules/lodash.camelcase": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", diff --git a/package.json b/package.json index f5fda9e..74d2706 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "typescript": "^5.5.4" }, "devDependencies": { - "jest": "^29.7.0" + "jest": "^29.7.0", + "lodash": "^4.17.21" } } diff --git a/tests/examples/googleAnalyticsArrowFunction.js b/tests/examples/googleAnalyticsArrowFunction.js new file mode 100644 index 0000000..1671e40 --- /dev/null +++ b/tests/examples/googleAnalyticsArrowFunction.js @@ -0,0 +1,8 @@ +const trackEvent = () => { + gtag('event', 'conversion', { + 'send_to': 'AW-XXXXXXX/XXXXXXXXXX', + 'value': 1.0, + 'currency': 'USD' + }); + }; +trackEvent(); \ No newline at end of file diff --git a/tests/examples/googleAnalyticsFunctionExpression.js b/tests/examples/googleAnalyticsFunctionExpression.js new file mode 100644 index 0000000..69dc7ee --- /dev/null +++ b/tests/examples/googleAnalyticsFunctionExpression.js @@ -0,0 +1,8 @@ +const trackEvent = function() { + gtag('event', 'conversion', { + 'send_to': 'AW-XXXXXXX/XXXXXXXXXX', + 'value': 1.0, + 'currency': 'USD' + }); +}; +trackEvent(); \ No newline at end of file diff --git a/tests/examples/googleAnalyticsGlobal.js b/tests/examples/googleAnalyticsGlobal.js new file mode 100644 index 0000000..906b834 --- /dev/null +++ b/tests/examples/googleAnalyticsGlobal.js @@ -0,0 +1,5 @@ +gtag('event', 'conversion', { + 'send_to': 'AW-XXXXXXX/XXXXXXXXXX', + 'value': 1.0, + 'currency': 'USD' +}); \ No newline at end of file diff --git a/tests/examples/googleAnalyticsRegularFunction.js b/tests/examples/googleAnalyticsRegularFunction.js new file mode 100644 index 0000000..54c16de --- /dev/null +++ b/tests/examples/googleAnalyticsRegularFunction.js @@ -0,0 +1,8 @@ +function trackEvent() { + gtag('event', 'conversion', { + 'send_to': 'AW-XXXXXXX/XXXXXXXXXX', + 'value': 1.0, + 'currency': 'USD' + }); + }; +trackEvent(); \ No newline at end of file diff --git a/tests/expected/googleAnalyticsArrowFunction.yaml b/tests/expected/googleAnalyticsArrowFunction.yaml new file mode 100644 index 0000000..80e6fdf --- /dev/null +++ b/tests/expected/googleAnalyticsArrowFunction.yaml @@ -0,0 +1,6 @@ +send_to: + type: string +value: + type: number +currency: + type: string diff --git a/tests/expected/googleAnalyticsFunctionExpression.yaml b/tests/expected/googleAnalyticsFunctionExpression.yaml new file mode 100644 index 0000000..80e6fdf --- /dev/null +++ b/tests/expected/googleAnalyticsFunctionExpression.yaml @@ -0,0 +1,6 @@ +send_to: + type: string +value: + type: number +currency: + type: string diff --git a/tests/expected/googleAnalyticsGlobal.yaml b/tests/expected/googleAnalyticsGlobal.yaml new file mode 100644 index 0000000..80e6fdf --- /dev/null +++ b/tests/expected/googleAnalyticsGlobal.yaml @@ -0,0 +1,6 @@ +send_to: + type: string +value: + type: number +currency: + type: string diff --git a/tests/expected/googleAnalyticsRegularFunction.yaml b/tests/expected/googleAnalyticsRegularFunction.yaml new file mode 100644 index 0000000..80e6fdf --- /dev/null +++ b/tests/expected/googleAnalyticsRegularFunction.yaml @@ -0,0 +1,6 @@ +send_to: + type: string +value: + type: number +currency: + type: string diff --git a/tests/googleAnalyticsJS.test.js b/tests/googleAnalyticsJS.test.js new file mode 100644 index 0000000..c729be7 --- /dev/null +++ b/tests/googleAnalyticsJS.test.js @@ -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); + } + }); +}); \ No newline at end of file