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

feat(extendsMappedType): make sure extending mapped types do not prevent to compile #241

Merged
merged 4 commits into from
Feb 16, 2020
Merged
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
4 changes: 3 additions & 1 deletion src/transformer/descriptor/mock/mockProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { PropertyLike } from './propertyLike';
import { SignatureLike } from './signatureLike';

export function GetMockPropertiesFromSymbol(propertiesSymbol: ts.Symbol[], signatures: ReadonlyArray<ts.Signature>, scope: Scope): ts.Expression {
const properties: PropertyLike[] = propertiesSymbol.map((prop: ts.Symbol) => prop.declarations[0]) as PropertyLike[];
const properties: PropertyLike[] = propertiesSymbol
.filter((prop: ts.Symbol) => !!prop.declarations) // Dynamically generated properties (mapped types) do not have declarations
.map((prop: ts.Symbol) => prop.declarations[0]) as PropertyLike[];

const signaturesDeclarations: SignatureLike[] = signatures.map((signature: ts.Signature) => signature.declaration) as SignatureLike[];

Expand Down
22 changes: 22 additions & 0 deletions test/transformer/descriptor/extends/mappedTypes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {createMock} from 'ts-auto-mock';

describe('extends MappedTypes', () => {
it('should not convert the properties', () => {
enum SOME_ENUM {
FIRST = 'FIRST',
SECOND = 'SECOND',
}


interface IBase {
propertyA: string;
propertyB: number;
}

interface InterfaceWithExtends extends Record<SOME_ENUM, IBase> {}

const type: InterfaceWithExtends = createMock<InterfaceWithExtends>();
expect(type.FIRST).toBeUndefined();
expect(type.SECOND).toBeUndefined();
});
});
29 changes: 29 additions & 0 deletions ui/src/views/types-not-supported.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,32 @@ Object.getOwnPropertySymbols(mock); //it should return the symbol testSymbol
```

TsAutoMock will create a property with the random name generated by typescript (example __@aSymbol) instead of the correct symbol.

## Extends Mapped Type
```ts

enum SOME_ENUM {
FIRST = 'FIRST',
SECOND = 'SECOND',
}

interface IBase {
someField: string;
anotherField: number;
}

interface InterfaceWithExtendsMappedType extends Record<SOME_ENUM, IBase[]> {}

const mock = createMock<InterfaceWithExtendsMappedType>();

mock.FIRST // undefined, it should be IBase[]
mock.SECOND // undefined, it should be IBase[]

[issue](https://github.com/Typescript-TDD/ts-auto-mock/issues/238)
Unfortunately this functionality doesnt work yet because when getting properties from an interface that extend
a mapped type typescript returns a different type of property that is difficult to mock.

There is a branch created with a working version but it needs more investigation because the implementation is not readable and it may cause more issues
[link](https://github.com/Typescript-TDD/ts-auto-mock/tree/feature/extends-mapped-type)

```