Skip to content

Commit

Permalink
updates to dev packages, bump ts version, pusher version
Browse files Browse the repository at this point in the history
  • Loading branch information
underfisk committed Jan 23, 2024
1 parent dda0f75 commit e34a736
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 358 deletions.
25 changes: 9 additions & 16 deletions src/__tests__/hello.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Controller, Get } from '@nestjs/common'
import { Request } from 'express'
import { Controller, Get } from '@nestjs/common';
import { Request } from 'express';

import {
PusherChannel,
PusherEvent,
PusherSendGuard,
PusherSocketId,
} from '../decorators'
import { PusherChannel, PusherEvent, PusherSendGuard, PusherSocketId } from '../decorators';

@Controller('hello')
export class HelloController {
Expand All @@ -17,7 +12,7 @@ export class HelloController {
@Get('channel-only')
@PusherChannel('world-channel')
world() {
return 'Hello, World'
return 'Hello, World';
}

/**
Expand All @@ -28,7 +23,7 @@ export class HelloController {
@PusherChannel('world-channel')
@PusherEvent('greetings')
channelAndEvent() {
return 'Hello, World'
return 'Hello, World';
}

/**
Expand All @@ -40,7 +35,7 @@ export class HelloController {
@PusherEvent('greetings')
@PusherSocketId('x-custom-sid')
customClientSocketIdHeader() {
return 'Hello, World'
return 'Hello, World';
}

/**
Expand All @@ -51,7 +46,7 @@ export class HelloController {
@PusherChannel((req: Request) => req.query['channel'].toString())
@PusherEvent('greetings')
channelNameBuilder() {
return 'May the Force be with you'
return 'May the Force be with you';
}

/**
Expand All @@ -61,10 +56,8 @@ export class HelloController {
@Get('send-guard')
@PusherChannel('world-channel')
@PusherEvent('greetings')
@PusherSendGuard((req: Request) =>
req.query['channel'].toString().startsWith('good-'),
)
@PusherSendGuard((req: Request) => req.query['channel'].toString().startsWith('good-'))
sendGuard() {
return 'Hello, World'
return 'Hello, World';
}
}
116 changes: 49 additions & 67 deletions src/__tests__/pusher.interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import * as request from 'supertest'
import { INestApplication, Logger } from '@nestjs/common'
import { APP_INTERCEPTOR } from '@nestjs/core'
import { Test } from '@nestjs/testing'
import { createSpyObj } from 'jest-createspyobj'
import * as request from 'supertest';
import { INestApplication, Logger } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { Test } from '@nestjs/testing';
import { createSpyObj } from 'jest-createspyobj';

import { PusherInterceptor } from '../pusher.interceptor'
import { PusherModule } from '../pusher.module'
import { PusherService } from '../pusher.service'
import { HelloController } from './hello.module'
import { PusherInterceptor } from '../pusher.interceptor';
import { PusherModule } from '../pusher.module';
import { PusherService } from '../pusher.service';
import { HelloController } from './hello.module';

describe('PusherInterceptor', () => {
let pusherService: jest.Mocked<PusherService>
let app: INestApplication
let pusherService: jest.Mocked<PusherService>;
let app: INestApplication;

beforeEach(async () => {
pusherService = createSpyObj(PusherService)
pusherService = createSpyObj(PusherService);
const testingModule = await Test.createTestingModule({
imports: [
PusherModule.forRoot({
Expand All @@ -36,98 +36,80 @@ describe('PusherInterceptor', () => {
useClass: PusherInterceptor,
},
],
}).compile()
}).compile();

app = testingModule.createNestApplication()
await app.init()
})
app = testingModule.createNestApplication();
await app.init();
});

describe('@PusherChannel only', () => {
it('should not send message if method is not decorated with PusherEvent', async () => {
const res = await request(app.getHttpServer()).get('/hello/channel-only')
expect(res.text).toBe('Hello, World')
expect(pusherService.trigger).not.toHaveBeenCalled()
})
})
const res = await request(app.getHttpServer()).get('/hello/channel-only');
expect(res.text).toBe('Hello, World');
expect(pusherService.trigger).not.toHaveBeenCalled();
});
});

describe('@PusherChannel + @PusherEvent', () => {
it('should send message if event name is available', async () => {
const res = await request(app.getHttpServer()).get(
'/hello/channel-and-event',
)
expect(res.text).toBe('Hello, World')
expect(pusherService.trigger).toHaveBeenCalledWith(
'world-channel',
'greetings',
'Hello, World',
undefined,
)
})
const res = await request(app.getHttpServer()).get('/hello/channel-and-event');
expect(res.text).toBe('Hello, World');
expect(pusherService.trigger).toHaveBeenCalledWith('world-channel', 'greetings', 'Hello, World', undefined);
});

it('should retrieve the channel name from builder', async () => {
const res = await request(app.getHttpServer()).get(
'/hello/channel-name-builder?channel=yay-my-new-channel',
)
expect(res.text).toBe('May the Force be with you')
const res = await request(app.getHttpServer()).get('/hello/channel-name-builder?channel=yay-my-new-channel');
expect(res.text).toBe('May the Force be with you');
expect(pusherService.trigger).toHaveBeenCalledWith(
'yay-my-new-channel',
'greetings',
'May the Force be with you',
undefined,
)
})
})
);
});
});

describe('@PusherSocketId', () => {
it('should use custom socketId header if provided', async () => {
const res = await request(app.getHttpServer())
.get('/hello/custom-sid-header')
.set({
['x-custom-sid']: 'custom-sid-123',
})
expect(res.text).toBe('Hello, World')
});
expect(res.text).toBe('Hello, World');
expect(pusherService.trigger).toHaveBeenCalledWith(
'world-channel',
'greetings',
'Hello, World',
'custom-sid-123',
)
})
);
});
it('should fall back on default socketId header', async () => {
const res = await request(app.getHttpServer())
.get('/hello/channel-and-event')
.set({
['x-pusher-sid']: 'pusher-sid-123',
})
expect(res.text).toBe('Hello, World')
});
expect(res.text).toBe('Hello, World');
expect(pusherService.trigger).toHaveBeenCalledWith(
'world-channel',
'greetings',
'Hello, World',
'pusher-sid-123',
)
})
})
);
});
});

describe('@PusherSendGuard', () => {
it('should send message if send guard returns true', async () => {
const res = await request(app.getHttpServer()).get(
'/hello/send-guard?channel=good-people',
)
expect(res.text).toBe('Hello, World')
expect(pusherService.trigger).toHaveBeenCalledWith(
'world-channel',
'greetings',
'Hello, World',
undefined,
)
})
const res = await request(app.getHttpServer()).get('/hello/send-guard?channel=good-people');
expect(res.text).toBe('Hello, World');
expect(pusherService.trigger).toHaveBeenCalledWith('world-channel', 'greetings', 'Hello, World', undefined);
});
it('should not send message if send guard returns false', async () => {
const res = await request(app.getHttpServer()).get(
'/hello/send-guard?channel=bad-people',
)
expect(res.text).toBe('Hello, World')
expect(pusherService.trigger).not.toHaveBeenCalled()
})
})
})
const res = await request(app.getHttpServer()).get('/hello/send-guard?channel=bad-people');
expect(res.text).toBe('Hello, World');
expect(pusherService.trigger).not.toHaveBeenCalled();
});
});
});
64 changes: 32 additions & 32 deletions src/__tests__/pusher.module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PusherService } from '../pusher.service'
import { Test } from '@nestjs/testing'
import { PusherModule } from '../pusher.module'
import { INestApplication } from '@nestjs/common'
import { PusherService } from '../pusher.service';
import { Test } from '@nestjs/testing';
import { PusherModule } from '../pusher.module';
import { INestApplication } from '@nestjs/common';

describe('PusherModule', () => {
// forRoot – legacy use case
describe('PusherModule.forRoot', () => {
let app: INestApplication
let app: INestApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
Expand All @@ -17,26 +17,26 @@ describe('PusherModule', () => {
cluster: 'us2',
}),
],
}).compile()
app = module.createNestApplication()
})
}).compile();
app = module.createNestApplication();
});

it('should configure PusherService', () => {
const pusherService = app.get<PusherService>(PusherService)
expect(pusherService).toBeDefined()
const pusherService = app.get<PusherService>(PusherService);
expect(pusherService).toBeDefined();

const { appId, token } = pusherService.getPusherInstance()['config']
const { key, secret } = token
const { appId, token } = pusherService.getPusherInstance()['config'];
const { key, secret } = token;

expect(appId).toBe('some-app-id')
expect(key).toBe('some-app-key')
expect(secret).toBe('some-app-secret')
})
})
expect(appId).toBe('some-app-id');
expect(key).toBe('some-app-key');
expect(secret).toBe('some-app-secret');
});
});

// forRootAsync: () => NestJsPusherAsyncOptions
describe('PusherModule.forRootAsync with NestJsPusherAsyncOptions', () => {
let asyncApp: INestApplication
let asyncApp: INestApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
Expand All @@ -50,24 +50,24 @@ describe('PusherModule', () => {
cluster: 'us2',
},
chunkingOptions: { limit: 1000, enabled: true },
}
};
},
}),
],
}).compile()
asyncApp = module.createNestApplication()
})
}).compile();
asyncApp = module.createNestApplication();
});

it('should configure PusherService', () => {
const pusherService = asyncApp.get<PusherService>(PusherService)
expect(pusherService).toBeDefined()
const pusherService = asyncApp.get<PusherService>(PusherService);
expect(pusherService).toBeDefined();

const { appId, token } = pusherService.getPusherInstance()['config']
const { key, secret } = token
const { appId, token } = pusherService.getPusherInstance()['config'];
const { key, secret } = token;

expect(appId).toBe('async-app-id')
expect(key).toBe('async-app-key')
expect(secret).toBe('async-app-secret')
})
})
})
expect(appId).toBe('async-app-id');
expect(key).toBe('async-app-key');
expect(secret).toBe('async-app-secret');
});
});
});
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PUSHER_EVENT = '__PUSHER-EVENT__'
export const PUSHER_SEND_GUARD = '__PUSHER-SEND-GUARD__'
export const PUSHER_CHANNEL = '__PUSHER-CHANNEL__'
export const PUSHER_SID_FACTORY = '__PUSHER-SID-FACTORY__'
export const PUSHER_EVENT = '__PUSHER-EVENT__';
export const PUSHER_SEND_GUARD = '__PUSHER-SEND-GUARD__';
export const PUSHER_CHANNEL = '__PUSHER-CHANNEL__';
export const PUSHER_SID_FACTORY = '__PUSHER-SID-FACTORY__';
Loading

0 comments on commit e34a736

Please sign in to comment.