Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
TuvalSimha committed Nov 12, 2024
1 parent a90e1d9 commit f1cd302
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 76 deletions.
24 changes: 16 additions & 8 deletions packages/core/__tests__/diff/directive-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,10 @@ describe('directive-usage', () => {

expect(changes.length).toEqual(1);
expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
expect(change.type).toEqual('DIRECTIVE_USAGE_ARGUMENT_DEFINITION_ADDED');
expect(change.message).toEqual("Directive 'external' was added to argument 'a'");
expect(change.type).toEqual('DIRECTIVE_USAGE_INPUT_FIELD_DEFINITION_ADDED');
expect(change.message).toEqual(
"Directive 'external' was added to input field 'a' in input object 'Foo'",
);
});
test('removed directive', async () => {
const a = buildSchema(/* GraphQL */ `
Expand All @@ -479,8 +481,10 @@ describe('directive-usage', () => {

expect(changes.length).toEqual(1);
expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
expect(change.type).toEqual('DIRECTIVE_USAGE_ARGUMENT_DEFINITION_REMOVED');
expect(change.message).toEqual("Directive 'external' was removed from input value 'Foo.a'");
expect(change.type).toEqual('DIRECTIVE_USAGE_INPUT_FIELD_DEFINITION_REMOVED');
expect(change.message).toEqual(
"Directive 'external' was removed from input field 'a' in input object 'Foo'",
);
});
});

Expand Down Expand Up @@ -634,7 +638,9 @@ describe('directive-usage', () => {

expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
expect(change.type).toEqual('DIRECTIVE_USAGE_ARGUMENT_DEFINITION_ADDED');
expect(change.message).toEqual("Directive 'external' was added to argument 'a'");
expect(change.message).toEqual(
"Directive 'external' was added to argument 'a' of field 'a' in type 'Foo'",
);
});

test('removed directive', async () => {
Expand All @@ -656,7 +662,9 @@ describe('directive-usage', () => {

expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
expect(change.type).toEqual('DIRECTIVE_USAGE_ARGUMENT_DEFINITION_REMOVED');
expect(change.message).toEqual("Directive 'external' was removed from argument 'Foo.a'");
expect(change.message).toEqual(
"Directive 'external' was removed from argument 'a' of field 'a' in type 'Foo'",
);
});
});

Expand Down Expand Up @@ -686,7 +694,7 @@ describe('directive-usage', () => {

expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
expect(change.type).toEqual('DIRECTIVE_USAGE_SCHEMA_ADDED');
expect(change.message).toEqual("Directive 'external' was added to schema");
expect(change.message).toEqual("Directive 'external' was added to schema 'Foo'");
});
test('removed directive', async () => {
const a = buildSchema(/* GraphQL */ `
Expand All @@ -713,7 +721,7 @@ describe('directive-usage', () => {

expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
expect(change.type).toEqual('DIRECTIVE_USAGE_SCHEMA_REMOVED');
expect(change.message).toEqual("Directive 'external' was removed from schema");
expect(change.message).toEqual("Directive 'external' was removed from schema 'Foo'");
});
});
});
120 changes: 52 additions & 68 deletions packages/core/src/diff/changes/directive-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ import {
DirectiveUsageUnionMemberRemovedChange,
} from './change.js';

function addedSpecialDirective(directiveName: string, forceReturn: CriticalityLevel) {
function addedSpecialDirective(
directiveName: string,
forceReturn: CriticalityLevel,
): CriticalityLevel {
if (directiveName === 'deprecated') {
return CriticalityLevel.NonBreaking;
}
Expand All @@ -51,7 +54,10 @@ function addedSpecialDirective(directiveName: string, forceReturn: CriticalityLe
return forceReturn;
}

function removedSpecialDirective(directiveName: string, forceReturn: CriticalityLevel) {
function removedSpecialDirective(
directiveName: string,
forceReturn: CriticalityLevel,
): CriticalityLevel {
if (directiveName === 'deprecated') {
return CriticalityLevel.NonBreaking;
}
Expand Down Expand Up @@ -251,7 +257,6 @@ export function directiveUsageInterfaceRemovedFromMeta(args: DirectiveUsageInter
} as const;
}

// ChangeType.DirectiveUsageInputFieldDefinitionAdded
function buildDirectiveUsageInputFieldDefinitionAddedMessage(
args: DirectiveUsageInputFieldDefinitionAddedChange['meta'],
): string {
Expand All @@ -275,7 +280,6 @@ export function directiveUsageInputFieldDefinitionAddedFromMeta(
} as const;
}

// ChangeType.DirectiveUsageInputFieldDefinitionRemoved
function buildDirectiveUsageInputFieldDefinitionRemovedMessage(
args: DirectiveUsageInputFieldDefinitionRemovedChange['meta'],
): string {
Expand Down Expand Up @@ -339,7 +343,6 @@ export function directiveUsageObjectRemovedFromMeta(args: DirectiveUsageObjectRe
} as const;
}

// ChangeType.DirectiveUsageEnumAdded
function buildDirectiveUsageEnumAddedMessage(args: DirectiveUsageEnumAddedChange['meta']): string {
return `Directive '${args.addedDirectiveName}' was added to enum '${args.enumName}'`;
}
Expand All @@ -357,7 +360,6 @@ export function directiveUsageEnumAddedFromMeta(args: DirectiveUsageEnumAddedCha
} as const;
}

// ChangeType.DirectiveUsageEnumRemoved
function buildDirectiveUsageEnumRemovedMessage(
args: DirectiveUsageEnumRemovedChange['meta'],
): string {
Expand All @@ -377,11 +379,10 @@ export function directiveUsageEnumRemovedFromMeta(args: DirectiveUsageEnumRemove
} as const;
}

// ChangeType.DirectiveUsageFieldDefinitionAdded
function buildDirectiveUsageFieldDefinitionAddedMessage(
args: DirectiveUsageFieldDefinitionAddedChange['meta'],
): string {
return `Directive '${args.addedDirectiveName}' was added to field '${args.fieldName}' in type '${args.typeName}'`;
return `Directive '${args.addedDirectiveName}' was added to field '${args.typeName}.${args.fieldName}'`;
}

export function directiveUsageFieldDefinitionAddedFromMeta(
Expand All @@ -399,11 +400,10 @@ export function directiveUsageFieldDefinitionAddedFromMeta(
} as const;
}

// ChangeType.DirectiveUsageFieldDefinitionRemoved
function buildDirectiveUsageFieldDefinitionRemovedMessage(
args: DirectiveUsageFieldDefinitionRemovedChange['meta'],
): string {
return `Directive '${args.removedDirectiveName}' was removed from field '${args.fieldName}' in type '${args.typeName}'`;
return `Directive '${args.removedDirectiveName}' was removed from field '${args.typeName}.${args.fieldName}'`;
}

export function directiveUsageFieldDefinitionRemovedFromMeta(
Expand All @@ -421,57 +421,6 @@ export function directiveUsageFieldDefinitionRemovedFromMeta(
} as const;
}

// ChangeType.DirectiveUsageUnionMemberAdded
function buildDirectiveUsageUnionMemberAddedMessage(
args: DirectiveUsageUnionMemberAddedChange['meta'],
): string {
return `Directive '${args.addedDirectiveName}' was added to union member '${args.addedUnionMemberTypeName}' in union '${args.unionName}'`;
}

export function directiveUsageUnionMemberAddedFromMeta(args: DirectiveUsageUnionMemberAddedChange) {
return {
criticality: {
level: addedSpecialDirective(args.meta.addedDirectiveName, CriticalityLevel.Dangerous),
reason: `Directive '${args.meta.addedDirectiveName}' was added to union member '${args.meta.addedUnionMemberTypeName}'`,
},
type: ChangeType.DirectiveUsageUnionMemberAdded,
message: buildDirectiveUsageUnionMemberAddedMessage(args.meta),
path: [
args.meta.unionName,
args.meta.addedUnionMemberTypeName,
args.meta.addedDirectiveName,
].join('.'),
meta: args.meta,
} as const;
}

// ChangeType.DirectiveUsageUnionMemberRemoved
function buildDirectiveUsageUnionMemberRemovedMessage(
args: DirectiveUsageUnionMemberRemovedChange['meta'],
): string {
return `Directive '${args.removedDirectiveName}' was removed from union member '${args.removedUnionMemberTypeName}' in union '${args.unionName}'`;
}

export function directiveUsageUnionMemberRemovedFromMeta(
args: DirectiveUsageUnionMemberRemovedChange,
) {
return {
criticality: {
level: removedSpecialDirective(args.meta.removedDirectiveName, CriticalityLevel.Dangerous),
reason: `Directive '${args.meta.removedDirectiveName}' was removed from union member '${args.meta.removedUnionMemberTypeName}'`,
},
type: ChangeType.DirectiveUsageUnionMemberRemoved,
message: buildDirectiveUsageUnionMemberRemovedMessage(args.meta),
path: [
args.meta.unionName,
args.meta.removedUnionMemberTypeName,
args.meta.removedDirectiveName,
].join('.'),
meta: args.meta,
} as const;
}

// ChangeType.DirectiveUsageEnumValueAdded
function buildDirectiveUsageEnumValueAddedMessage(
args: DirectiveUsageEnumValueAddedChange['meta'],
): string {
Expand All @@ -491,7 +440,6 @@ export function directiveUsageEnumValueAddedFromMeta(args: DirectiveUsageEnumVal
} as const;
}

// ChangeType.DirectiveUsageEnumValueRemoved
function buildDirectiveUsageEnumValueRemovedMessage(
args: DirectiveUsageEnumValueRemovedChange['meta'],
): string {
Expand All @@ -511,7 +459,6 @@ export function directiveUsageEnumValueRemovedFromMeta(args: DirectiveUsageEnumV
} as const;
}

// ChangeType.DirectiveUsageSchemaAdded
function buildDirectiveUsageSchemaAddedMessage(
args: DirectiveUsageSchemaAddedChange['meta'],
): string {
Expand All @@ -531,7 +478,6 @@ export function directiveUsageSchemaAddedFromMeta(args: DirectiveUsageSchemaAdde
} as const;
}

// ChangeType.DirectiveUsageSchemaRemoved
function buildDirectiveUsageSchemaRemovedMessage(
args: DirectiveUsageSchemaRemovedChange['meta'],
): string {
Expand All @@ -551,7 +497,6 @@ export function directiveUsageSchemaRemovedFromMeta(args: DirectiveUsageSchemaRe
} as const;
}

// ChangeType.DirectiveUsageScalarAdded
function buildDirectiveUsageScalarAddedMessage(
args: DirectiveUsageScalarAddedChange['meta'],
): string {
Expand All @@ -571,7 +516,6 @@ export function directiveUsageScalarAddedFromMeta(args: DirectiveUsageScalarAdde
} as const;
}

// ChangeType.DirectiveUsageScalarRemoved
function buildDirectiveUsageScalarRemovedMessage(
args: DirectiveUsageScalarRemovedChange['meta'],
): string {
Expand All @@ -591,6 +535,46 @@ export function directiveUsageScalarRemovedFromMeta(args: DirectiveUsageScalarRe
} as const;
}

function buildDirectiveUsageUnionMemberAddedMessage(
args: DirectiveUsageUnionMemberAddedChange['meta'],
): string {
return `Directive '${args.addedDirectiveName}' was added to union member '${args.unionName}'`;
}

export function directiveUsageUnionMemberAddedFromMeta(args: DirectiveUsageUnionMemberAddedChange) {
return {
criticality: {
level: addedSpecialDirective(args.meta.addedDirectiveName, CriticalityLevel.Dangerous),
reason: `Directive '${args.meta.addedDirectiveName}' was added to union member '${args.meta.unionName}.${args.meta.addedUnionMemberTypeName}'`,
},
type: ChangeType.DirectiveUsageUnionMemberAdded,
message: buildDirectiveUsageUnionMemberAddedMessage(args.meta),
path: [args.meta.unionName, args.meta.addedDirectiveName].join('.'),
meta: args.meta,
} as const;
}

function buildDirectiveUsageUnionMemberRemovedMessage(
args: DirectiveUsageUnionMemberRemovedChange['meta'],
): string {
return `Directive '${args.removedDirectiveName}' was removed from union member '${args.unionName}'`;
}

export function directiveUsageUnionMemberRemovedFromMeta(
args: DirectiveUsageUnionMemberRemovedChange,
) {
return {
criticality: {
level: removedSpecialDirective(args.meta.removedDirectiveName, CriticalityLevel.Dangerous),
reason: `Directive '${args.meta.removedDirectiveName}' was removed from union member '${args.meta.unionName}.${args.meta.removedUnionMemberTypeName}'`,
},
type: ChangeType.DirectiveUsageUnionMemberRemoved,
message: buildDirectiveUsageUnionMemberRemovedMessage(args.meta),
path: [args.meta.unionName, args.meta.removedDirectiveName].join('.'),
meta: args.meta,
} as const;
}

export function directiveUsageAdded<K extends keyof KindToPayload>(
kind: K,
directive: ConstDirectiveNode,
Expand Down Expand Up @@ -622,7 +606,7 @@ export function directiveUsageAdded<K extends keyof KindToPayload>(
type: ChangeType.DirectiveUsageInputObjectAdded,
meta: {
addedDirectiveName: directive.name.value,
addedInputFieldName: payload.getFields()[0].name,
addedInputFieldName: directive.name.value,
addedInputFieldType: payload.name,
inputObjectName: payload.name,
isAddedInputFieldTypeNullable: kind === Kind.INPUT_VALUE_DEFINITION,
Expand Down Expand Up @@ -739,7 +723,7 @@ export function directiveUsageRemoved<K extends keyof KindToPayload>(
type: ChangeType.DirectiveUsageInputObjectRemoved,
meta: {
removedDirectiveName: directive.name.value,
removedInputFieldName: payload.getFields()[0].name,
removedInputFieldName: directive.name.value,
removedInputFieldType: payload.name,
inputObjectName: payload.name,
isRemovedInputFieldTypeNullable: kind === Kind.INPUT_VALUE_DEFINITION,
Expand Down

0 comments on commit f1cd302

Please sign in to comment.