Skip to content

Commit 1430c22

Browse files
authored
Merge pull request #932
Update with release/2025.1.2
2 parents 089aaea + 8836601 commit 1430c22

File tree

3 files changed

+123
-93
lines changed

3 files changed

+123
-93
lines changed

Source/dom/models/Library.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,12 @@ export class Library extends WrappedObject {
174174
const collector = MSForeignObjectCollector.alloc().initWithProvider(
175175
provider
176176
)
177-
const shareableObjectRefsMap = collector.buildCollectionWithFilter(null)
178-
179-
const currentId = this.id
180-
const currentName = this.name
181-
182-
const shareableObjectRefsForCurrentLib = toArray(
183-
shareableObjectRefsMap
184-
).find(
185-
(o) =>
186-
o.library &&
187-
String(o.library.libraryID()) === currentId &&
188-
String(o.library.name()) === currentName
177+
const shareableObjectRefs = toArray(
178+
collector.collectImportableReferencesFromLibrary(this.sketchObject)
189179
)
190180

191-
if (!shareableObjectRefsForCurrentLib) {
192-
return []
193-
}
194181
const documentData = document._getMSDocumentData()
195-
return toArray(shareableObjectRefsForCurrentLib.objectRefs).map((ref) => {
182+
return toArray(shareableObjectRefs).map((ref) => {
196183
const obj = ImportableObject.fromNative(ref)
197184
obj._documentData = documentData
198185
return obj

Source/dom/models/__tests__/Library.test.js

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ function createLibrary(testOutputPath = outputPath()) {
4242
}
4343

4444
test('should create a library from a document', () => {
45-
createLibrary().then((lib) => {
46-
expect(lib.type).toBe('Library')
47-
expect(getLibraries().find((d) => d.id === lib.id)).toEqual(lib)
45+
return createLibrary().then((lib) => {
46+
try {
47+
expect(lib.type).toBe('Library')
48+
expect(getLibraries().find((d) => d.id === lib.id)).toEqual(lib)
49+
} finally {
50+
lib.remove()
51+
}
4852
})
4953
})
5054

@@ -55,49 +59,94 @@ test('should list the libraries', () => {
5559
expect(libraries[0].type).toBe('Library')
5660
})
5761

58-
test('should be able to get the list of symbols to be imported', (_context, document) => {
59-
createLibrary().then((lib) => {
60-
expect(lib.getImportableSymbolReferencesForDocument(document)[0].type).toBe(
61-
'ImportableObject'
62-
)
62+
test('should be able to get the list of symbols to be imported', () => {
63+
return createLibrary().then((lib) => {
64+
try {
65+
// Library.getImportableSymbolReferencesForDocument() and its sibling methods all expect
66+
// an actual Document as an argument, not a DocumentData we get from the test environment
67+
const document = new Document()
68+
const importableSymbols = lib.getImportableSymbolReferencesForDocument(
69+
document
70+
)
71+
expect(importableSymbols[0].type).toBe('ImportableObject')
72+
expect(importableSymbols[0].name).toBe('Test')
73+
expect(importableSymbols[0].objectType).toBe(
74+
Library.ImportableObjectType.Symbol
75+
)
76+
} finally {
77+
lib.remove()
78+
}
79+
})
80+
})
81+
82+
test('should return references to already imported local copies of symbols if they exist', () => {
83+
return createLibrary().then((lib) => {
84+
try {
85+
const document = new Document()
86+
const refBeforeImport = lib.getImportableSymbolReferencesForDocument(
87+
document
88+
)[0]
89+
expect(refBeforeImport.sketchObject.remoteReference()).toBeNull()
90+
91+
refBeforeImport.import()
92+
93+
const refAfterImport = lib.getImportableSymbolReferencesForDocument(
94+
document
95+
)[0]
96+
expect(refAfterImport.sketchObject.remoteReference()).not.toBeNull()
97+
} finally {
98+
lib.remove()
99+
}
63100
})
64101
})
65102

66103
test('should disable a library', () => {
67-
createLibrary().then((lib) => {
68-
expect(lib.enabled).toBe(true)
69-
lib.enabled = false
70-
expect(lib.enabled).toBe(false)
71-
lib.enabled = true
72-
expect(lib.enabled).toBe(true)
104+
return createLibrary().then((lib) => {
105+
try {
106+
expect(lib.enabled).toBe(true)
107+
lib.enabled = false
108+
expect(lib.enabled).toBe(false)
109+
lib.enabled = true
110+
expect(lib.enabled).toBe(true)
111+
} finally {
112+
lib.remove()
113+
}
73114
})
74115
})
75116

76117
test('should get the lastModifiedAt date', () => {
77-
createLibrary().then((lib) => {
78-
expect(lib.lastModifiedAt instanceof Date).toBe(true)
118+
return createLibrary().then((lib) => {
119+
try {
120+
expect(lib.lastModifiedAt instanceof Date).toBe(true)
121+
} finally {
122+
lib.remove()
123+
}
79124
})
80125
})
81126

82127
test('should get the document of the library', () => {
83128
const testOutputPath = outputPath()
84-
85-
createLibrary(testOutputPath).then((lib) => {
86-
const libDocument = lib.getDocument()
87-
88-
expect(libDocument.type).toBe('Document')
89-
expect(libDocument.path).toBe(
90-
String(
91-
NSString.stringWithString(
92-
`${testOutputPath}/sketch-api-unit-tests-library.sketch`
129+
130+
return createLibrary(testOutputPath).then((lib) => {
131+
try {
132+
const libDocument = lib.getDocument()
133+
134+
expect(libDocument.type).toBe('Document')
135+
expect(libDocument.path).toBe(
136+
String(
137+
NSString.stringWithString(
138+
`${testOutputPath}/sketch-api-unit-tests-library.sketch`
139+
)
93140
)
94141
)
95-
)
142+
} finally {
143+
lib.remove()
144+
}
96145
})
97146
})
98147

99148
test('should remove a library', () => {
100-
createLibrary().then((lib) => {
149+
return createLibrary().then((lib) => {
101150
lib.remove()
102151

103152
expect(getLibraries().find((d) => d.id === lib.id)).toBe(undefined)

webpack.tests.config.js

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -185,49 +185,48 @@ function source(identifier, tests) {
185185
// Runs all test suites
186186
const numSuites = Object.entries(suites).length
187187
let results = []
188+
let promise = Promise.resolve()
188189

189190
Object.entries(suites).forEach(([suiteTitle, val], index) => {
190191
const numTests = Object.entries(val.source.tests).length
191192

192193
Object.entries(val.source.tests).forEach(([title, test], testIndex) => {
193-
console.log(`Running test: ${suiteTitle} ${title}`)
194-
195-
let status = 'pending'
196-
let failureReason
197-
198-
try {
199-
expect.resetAssertionsLocalState()
200-
201-
// All tests are given the plugin command context and a new document data
202-
// object.
203-
//
204-
// Note: This is not a full Document instance wrapping MSDocument. Using
205-
// MSDocument slows down tests massively, so we rely on the private API
206-
// creating document data from MSDocumentData.
207-
test(context, createDocumentData())
208-
status = 'passed'
209-
} catch (err) {
210-
status = 'failed'
211-
failureReason = getTestFailure(err)
212-
}
213-
214-
const fraction =
215-
index / numSuites + (testIndex + 1) / numTests / numSuites
216-
217-
results.push({
218-
ancestorTitles: [suiteTitle], // we don't have nested test suites in the API but sticking to Jest types anyway.
219-
fullName: `${suiteTitle} ${title}`,
220-
status,
221-
title,
222-
relativePath: `.${val.path.split(/SketchAPI/)[1]}`,
223-
failureReason,
224-
})
225-
226-
if (!onProgress) return
227-
228-
onProgress({ fraction, results })
194+
promise = promise
195+
.then(() => {
196+
console.log(`Running test: ${suiteTitle} ${title}`)
197+
return test(context, createDocumentData())
198+
})
199+
.then(() => {
200+
results.push({
201+
ancestorTitles: [suiteTitle],
202+
fullName: `${suiteTitle} ${title}`,
203+
status: 'passed',
204+
title,
205+
relativePath: `.${val.path.split(/SketchAPI/)[1]}`,
206+
failureReason: undefined,
207+
})
208+
})
209+
.catch((err) => {
210+
results.push({
211+
ancestorTitles: [suiteTitle],
212+
fullName: `${suiteTitle} ${title}`,
213+
status: 'failed',
214+
title,
215+
relativePath: `.${val.path.split(/SketchAPI/)[1]}`,
216+
failureReason: getTestFailure(err),
217+
})
218+
})
219+
.then(() => {
220+
if (onProgress) {
221+
const fraction =
222+
index / numSuites + (testIndex + 1) / numTests / numSuites
223+
onProgress({ fraction, results })
224+
}
225+
})
229226
})
230227
})
228+
229+
return promise
231230
}
232231

233232
return `
@@ -298,7 +297,10 @@ function source(identifier, tests) {
298297
299298
console.log(\`🏇 Running \${Object.keys(testSuites).length} test suites…\`)
300299
301-
const result = runner({
300+
const { createFiber } = require('sketch/async')
301+
const fiber = createFiber();
302+
303+
runner({
302304
context,
303305
expect,
304306
suites: testSuites,
@@ -314,19 +316,11 @@ function source(identifier, tests) {
314316
},
315317
prepareStackTrace
316318
})
317-
318-
const data = NSString.alloc().initWithString(JSON.stringify(result, null, 2))
319-
const err = MOPointer.alloc().init()
320-
321-
data.writeToFile_atomically_encoding_error(
322-
output,
323-
false,
324-
NSUTF8StringEncoding,
325-
err
326-
)
327-
328-
console.log('✅ Test results saved to: ' + output)
329-
sketch.UI.message('✅ Test results saved to disk.')
319+
.finally(() => {
320+
console.log('✅ Test results saved to: ' + output)
321+
sketch.UI.message('✅ Test results saved to disk.')
322+
fiber.cleanup()
323+
})
330324
}
331325
`
332326
}

0 commit comments

Comments
 (0)