Skip to content

Commit

Permalink
refactor: code smells 25july2024 (#46)
Browse files Browse the repository at this point in the history
* refactor(test): remove deep nesting

* refactor(test): remove deep nesting

* refactor(test): remove deep nesting

* refactor(sonarcloud): resolve code smells

* style: prettier formating

* style: remove unused import
  • Loading branch information
elersong authored Jul 25, 2024
1 parent 2b1cdd1 commit 6067995
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 231 deletions.
102 changes: 50 additions & 52 deletions src/BaseFirestoreRepository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('BaseFirestoreRepository', () => {
let firestore: Firestore;

beforeEach(() => {
const fixture = Object.assign({}, getFixture());
const fixture = { ...getFixture() };
const firebase = new MockFirebase(fixture, {
isNaiveSnapshotListenerEnabled: false,
});
Expand Down Expand Up @@ -84,63 +84,61 @@ describe('BaseFirestoreRepository', () => {
it.todo('must throw if the limit is less than 0');
});

describe('ordering', () => {
describe('orderByAscending', () => {
it('must order repository objects', async () => {
const bands = await bandRepository.orderByAscending('formationYear').find();
expect(bands[0].id).toEqual('the-speckled-band');
});
describe('orderByAscending', () => {
it('must order repository objects', async () => {
const bands = await bandRepository.orderByAscending('formationYear').find();
expect(bands[0].id).toEqual('the-speckled-band');
});

it('must order the objects in a subcollection', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
const discographyNewestFirst = await albumsSubColl?.orderByAscending('releaseDate').find();
if (discographyNewestFirst) {
expect(discographyNewestFirst[0].id).toEqual('lightbulb-sun');
}
});
it('must order the objects in a subcollection', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
const discographyNewestFirst = await albumsSubColl?.orderByAscending('releaseDate').find();
if (discographyNewestFirst) {
expect(discographyNewestFirst[0].id).toEqual('lightbulb-sun');
}
});

it('must be chainable with where* filters', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
const discographyNewestFirst = await albumsSubColl
?.whereGreaterOrEqualThan('releaseDate', new Date('2001-01-01'))
.orderByAscending('releaseDate')
.find();
if (discographyNewestFirst) {
expect(discographyNewestFirst[0].id).toEqual('in-absentia');
}
});
it('must be chainable with where* filters', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
const discographyNewestFirst = await albumsSubColl
?.whereGreaterOrEqualThan('releaseDate', new Date('2001-01-01'))
.orderByAscending('releaseDate')
.find();
if (discographyNewestFirst) {
expect(discographyNewestFirst[0].id).toEqual('in-absentia');
}
});

it('must be chainable with limit', async () => {
const bands = await bandRepository.orderByAscending('formationYear').limit(3).find();
const lastBand = bands[bands.length - 1];
expect(lastBand.id).toEqual('red-hot-chili-peppers');
});
it('must be chainable with limit', async () => {
const bands = await bandRepository.orderByAscending('formationYear').limit(3).find();
const lastBand = bands[bands.length - 1];
expect(lastBand.id).toEqual('red-hot-chili-peppers');
});

it('must throw an Error if an orderBy* function is called more than once in the same expression', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
expect(() => {
albumsSubColl?.orderByAscending('releaseDate').orderByDescending('releaseDate');
}).toThrow();
});
it('must throw an Error if an orderBy* function is called more than once in the same expression', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
expect(() => {
albumsSubColl?.orderByAscending('releaseDate').orderByDescending('releaseDate');
}).toThrow();
});

it('must throw an Error if an orderBy* function is called more than once in the same expression ascending', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
expect(() => {
albumsSubColl?.orderByAscending('releaseDate').orderByAscending('releaseDate');
}).toThrow();
});
it('must throw an Error if an orderBy* function is called more than once in the same expression ascending', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
expect(() => {
albumsSubColl?.orderByAscending('releaseDate').orderByAscending('releaseDate');
}).toThrow();
});

it('must succeed when orderBy* function is called more than once in the same expression with different fields', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
expect(() => {
albumsSubColl?.orderByAscending('releaseDate').orderByDescending('name');
}).not.toThrow();
});
it('must succeed when orderBy* function is called more than once in the same expression with different fields', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt?.albums;
expect(() => {
albumsSubColl?.orderByAscending('releaseDate').orderByDescending('name');
}).not.toThrow();
});
});

Expand Down
Loading

0 comments on commit 6067995

Please sign in to comment.