Skip to content

Commit 95ef70c

Browse files
committed
add more "this" tests
1 parent 4d2bddf commit 95ef70c

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/__tests__/analytics-pre-init.test.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { Analytics } from '../analytics'
99
import { Context } from '../core/context'
1010
import { sleep } from './test-helpers/sleep'
1111

12+
afterEach(() => {
13+
jest.clearAllMocks()
14+
})
15+
1216
describe('PreInitMethodCallBuffer', () => {
1317
describe('push', () => {
1418
it('should return this', async () => {
@@ -75,6 +79,19 @@ describe('AnalyticsBuffered', () => {
7579
expect(typeof buffered.finally).toBe('function')
7680
})
7781

82+
it('should have instance and ctx properties defined when loader is done', async () => {
83+
const ajs = new Analytics({ writeKey: 'foo' })
84+
const ctx = new Context({ type: 'track' })
85+
const buffered = new AnalyticsBuffered(() =>
86+
Promise.resolve<[Analytics, Context]>([ajs, ctx])
87+
)
88+
expect(buffered.instance).not.toBeDefined()
89+
expect(buffered.ctx).not.toBeDefined()
90+
await buffered // finish loading
91+
expect(buffered.instance).toBe(ajs)
92+
expect(buffered.ctx).toBe(ctx)
93+
})
94+
7895
it('should convert to a promise on await', async () => {
7996
const ajs = new Analytics({ writeKey: 'foo' })
8097
const ctx = new Context({ type: 'track' })
@@ -85,6 +102,20 @@ describe('AnalyticsBuffered', () => {
85102
expect(analytics).toEqual(ajs)
86103
expect(context).toEqual(ctx)
87104
})
105+
106+
it('should set the "this" value of any proxied analytics methods to the analytics instance', async () => {
107+
const ajs = new Analytics({ writeKey: 'foo' })
108+
jest.spyOn(ajs, 'track').mockImplementation(function (this: Analytics) {
109+
expect(this).toBe(ajs)
110+
return Promise.resolve(ctx)
111+
})
112+
const ctx = new Context({ type: 'track' })
113+
const result: [Analytics, Context] = [ajs, ctx]
114+
const buffered = new AnalyticsBuffered(() => Promise.resolve(result))
115+
await buffered // finish loading
116+
void buffered.track('foo', {})
117+
expect.assertions(1)
118+
})
88119
})
89120

90121
describe('Unhappy path', () => {
@@ -144,10 +175,6 @@ describe('callAnalyticsMethod', () => {
144175
writeKey: 'abc',
145176
})
146177
})
147-
afterEach(() => {
148-
jest.clearAllMocks()
149-
})
150-
151178
it('should change called to true', async () => {
152179
methodCall.called = false
153180
await callAnalyticsMethod(ajs, methodCall)

src/analytics-pre-init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export class AnalyticsBuffered implements PromiseLike<[Analytics, Context]> {
277277
): Promise<ReturnTypeUnwrap<Analytics[T]>> => {
278278
if (this.instance) {
279279
const method = this.instance[methodName] as Function
280-
return method(...args)
280+
return method.call(this.instance, ...args)
281281
}
282282

283283
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)